Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String.FormatException with DateTime in non US Culture

I am getting a String.FormatException trying to convert/parse a string when the culture is other than non-US. The odd thing is that the string was generated by applying the very same format and culture as those being used to parse it back into a string. In the code below, all of these versions will fail:

const string culture = "ja-JP";
const string format = "dd MMM yyyy"; //error in orignal post included {0:}

CultureInfo info = new CultureInfo(culture);
Thread.CurrentThread.CurrentCulture = info;
Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture(culture);

//string toParse = String.Format(info, format, DateTime.Now); //error in original post
string toParse = DateTime.Now.ToString(format);
System.Diagnostics.Debug.WriteLine(string.Format("Culture format = {0}, Date = {1}", culture, toParse));
try
{
    DateTime output = DateTime.ParseExact(toParse, format, CultureInfo.InvariantCulture);
    //DateTime output = DateTime.ParseExact(toParse, format, info);
    //DateTime output = DateTime.ParseExact(toParse, format, info, DateTimeStyles.None);
    //DateTime output = Convert.ToDateTime(toParse, info);
}
catch (Exception ex)
{
    System.Diagnostics.Debug.WriteLine(ex.Message);
}

The string for en-US is "25 Feb 2010".
The string for ja-JP is "25 2 2010".

Any idea how to get "25 2 2010" back into a date?

Thanks in advance.

Edit 1: I should note that the Japanese culture is hard-coded here only as an example. I really need this to work with whatever culture is set by the user. What I need is a solution where the date time format works no matter what the user's culture. I think the single M does it.

Edit 2: M doesn't work for English. Anyone know a format string that works for all cultures?

like image 875
sydneyos Avatar asked Feb 25 '10 22:02

sydneyos


People also ask

What is string date/time format?

A date and time format string defines the text representation of a DateTime or DateTimeOffset value that results from a formatting operation. It can also define the representation of a date and time value that is required in a parsing operation in order to successfully convert the string to a date and time.

Is date a string format?

A date and time format string is a string of text used to interpret data values containing date and time information. Each format string consists of a combination of formats from an available format type. Some examples of format types are day of week, month, hour, and second.

What is the default format of DateTime?

DATETIME type is a date and time combination, and stores data in YYYY-MM-DD HH:MM:SS format.


1 Answers

If you change:

DateTime output = DateTime.ParseExact(
    toParse, format, CultureInfo.InvariantCulture);

to

DateTime output = DateTime.ParseExact(toParse, "dd MMM yyyy", info);

the date is correctly parsed.

Note that in your example you are using a culture (ja-JP) to convert to string but another culture to convert from string. Another problem is that String.Format accepts a composite format string ("My string to format - {0:dd MMM yyyy}"), but DateTime.ParseExact is expecting only the date time format.

like image 141
João Angelo Avatar answered Oct 23 '22 10:10

João Angelo