String.FormatException with DateTime in non US Culture [英] String.FormatException with DateTime in non US Culture

查看:118
本文介绍了String.FormatException with DateTime in non US Culture的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当文化不是非美国时,我会收到一个String.FormatException尝试转换/解析字符串。奇怪的是,通过应用与用于将其解析成字符串的格式和文化相同的格式和文化,字符串生成。在下面的代码中,所有这些版本将失败:

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);
}

请注意,en-US的字符串为2010年2月25日。 ja-JP的字符串是25 2 2010。

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

任何想法如何将25 2 2010重新设置为日期?

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

提前感谢

编辑1:我应该注意,日本文化在这里只是一个硬编码的例子。我真的需要这个来处理用户设置的任何文化。我需要的是一个解决方案,日期时间格式的工作,无论用户的文化。我认为单一的M是这样的。

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.

编辑2:M不适用于英语。任何人都知道适用于所有文化的格式字符串?

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

推荐答案

如果您更改:

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

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

日期已正确解析。

请注意,在您的示例中,您正在使用文化(ja-JP)转换为字符串,但另一种文化要从字符串转换。另一个问题是 String.Format 接受复合格式字符串(我的字符串格式 - {0:dd MMM yyyy}),但 DateTime.ParseExact 只期待日期时间格式。

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.

这篇关于String.FormatException with DateTime in non US Culture的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆