I am converting DateTime to a string in en-US culture.
dateTimeString = dateTime.ToString();
But if I start my app in fr-FR culture and try to parse using below statement
DateTime.Parse(dateTimeString, CultureInfo.CurrentCulture);
It throws FormatException.
I am missing something?
Yes, that’s going to be a problem.
The regular
ToString()on a DateTime will generate a date string like this, foren-US:For
fr-FRit will generate this instead:So, if you try to parse the first string (en-US) as a fr-FR date time string, the 26 would be considered an invalid month and a
FormatExceptionis expected.EDIT:
Date/times can be a bit of a pain to work with. For portability (across culture formats and timezones), if you need to serialize as a string, I’d recommend serializing in
ISO 8601format.