Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert general SQL time format in c# ( all variations )?

If I have theses general sql valid dates in c# strings :

(each date can be in different order inside ) e.g. :

01 feb 2010
feb 01 2010
2010 01 feb
...
...

I want to convert a string date to DateTime(c#). ( i want the ability to convert every format from above list)

while parse exact requires me 3! combinations , is there any better solution ?

like image 753
Royi Namir Avatar asked Nov 03 '22 21:11

Royi Namir


1 Answers

I think i got it ... enter image description here

string[] str = new[] { "01 feb 2010","feb 01 2010","2010 01 feb","2010 feb 01","feb 2010 01" };
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-US");
CultureInfo culture = CultureInfo.CreateSpecificCulture("en-US");                       
for (int i = 0; i < str.Length; i++)
{
    DateTime t = DateTime.Parse(str[i], culture);
    t.Dump();
}
like image 167
Royi Namir Avatar answered Nov 10 '22 00:11

Royi Namir