Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CultureInfo.GetCultureInfo("en-GB") doesn't compatible with en-US date format but fines with others

Why the first line of code throws "String was not recognized as a valid DateTime." exception?

en-GB seems not compatible with en-US but strangely enough compatible with zh-CN and ru-RU date format string.

Yes I could simply change the culture info to en-US to make it compatible with en-US date format string. But my question is why en-GB works with zh-CN and ru-RU format strings?

any ideas?

        var obj = DateTime.Parse("11/20/2013 3:10:36 AM", CultureInfo.GetCultureInfo(0x0809).DateTimeFormat);//en-US
        obj = DateTime.Parse("2013/11/20 3:16:08", CultureInfo.GetCultureInfo(0x0809).DateTimeFormat);//zh-CN
        obj = DateTime.Parse("20.11.2013 3:17:44", CultureInfo.GetCultureInfo(0x0809).DateTimeFormat); //ru-RU

0x0809 en-GB English (United Kingdom) English

Thanks,

like image 357
Lys Avatar asked Dec 25 '22 17:12

Lys


1 Answers

You have specified that you are expecting a DateFormat in en-GB which is usually in the format dd/MM/yyyy ... and it is trying to create a date with a Month value of 20, which does not exist.

If you change your expected input format to en-US it will work and your internal date will be correct. You can then output it to the en-GB format if you wish

var obj = DateTime.Parse("11/20/2013 3:10:36 AM", CultureInfo.GetCultureInfo("en-US").DateTimeFormat);//en-US
string s = obj.ToString(CultureInfo.GetCultureInfo("en-GB").DateTimeFormat); //20/11/2013 03:10:36
like image 195
Xenolightning Avatar answered Dec 28 '22 06:12

Xenolightning