如何生产本地化日期字符串的CultureInfo [英] How to produce localized date string with CultureInfo
问题描述
我有一个生产日期字符串EN-US格式如下code。我想通过在LCID(或等值的本地化语言)来生成日期字符串的本地化版本。我将如何做到这一点?
公共静态字符串ConvertDateTimeToDate(字符串dateTimeString){
CultureInfo的文化= CultureInfo.InvariantCulture;
DateTime的DT = DateTime.MinValue;
如果(DateTime.TryParse(dateTimeString,出DT))
{
返回dt.ToShortDateString();
}
返回dateTimeString;
}
您可以使用第二个参数的的toString 的功能和使用任何语言/文化,你需要...
您可以使用D格式,而不是 ToShortDateString
根据MSDN ...
因此,基本上是这样的,返回的澳大利亚英语:
的CultureInfo enAU =新的CultureInfo(EN-AU);
dt.ToString(D,enAU);
您可以修改你的方法,包括语言和文化作为一个参数
公共静态字符串ConvertDateTimeToDate(字符串dateTimeString,字符串langCulture){
CultureInfo的文化=新的CultureInfo(langCulture);
DateTime的DT = DateTime.MinValue;
如果(DateTime.TryParse(dateTimeString,出DT))
{
返回dt.ToString(D,文化);
}
返回dateTimeString;
}
修改 I have the following code that produces a date string in en-us format. I would like to pass in the LCID (or equivalent value for the localized language) to produce the localized version of the date string. How would I accomplish this?
You can use the second argument to the toString function and use any language/culture you need... You can use the "d" format instead of So basically something like this to return as Australian English: you could modify your method to include the language and culture as a parameter Edit 这篇关于如何生产本地化日期字符串的CultureInfo的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
您可能也想看看href="http://msdn.microsoft.com/en-us/library/9h21f14e.aspx">重载的TryParse方法的public static string ConvertDateTimeToDate(string dateTimeString) {
CultureInfo culture = CultureInfo.InvariantCulture;
DateTime dt = DateTime.MinValue;
if (DateTime.TryParse(dateTimeString, out dt))
{
return dt.ToShortDateString();
}
return dateTimeString;
}
ToShortDateString
according to MSDN...CultureInfo enAU = new CultureInfo("en-AU");
dt.ToString("d", enAU);
public static string ConvertDateTimeToDate(string dateTimeString, String langCulture) {
CultureInfo culture = new CultureInfo(langCulture);
DateTime dt = DateTime.MinValue;
if (DateTime.TryParse(dateTimeString, out dt))
{
return dt.ToString("d",culture);
}
return dateTimeString;
}
You may also want to look at the overloaded tryParse method if you need to parse the string against a particular language/culture...