如何使用 CultureInfo 生成本地化日期字符串
我有以下代码,可以生成 en-us 格式的日期字符串。我想传递 LCID(或本地化语言的等效值)以生成日期字符串的本地化版本。我将如何实现这个目标?
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;
}
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?
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 toString 函数的第二个参数并使用任何您需要的语言/文化...
您可以根据 MSDN 使用“d”格式而不是
ToShortDateString
...所以基本上类似这样的内容返回为澳大利亚英语:
您可以修改您的方法以包括语言和文化作为参数
编辑
如果需要,您可能还想查看重载的 tryParse 方法根据特定的语言/文化解析字符串...
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
ToShortDateString
according to MSDN...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
You may also want to look at the overloaded tryParse method if you need to parse the string against a particular language/culture...
使用
ToString()
重载而不是ToShortDateString()
方法。提供一个IFormatProvider
。这应该有助于形成特定的日期时间字符串:
http://www. csharp-examples.net/string-format-datetime/
这应该对解决本地化问题有帮助:
您如何处理本地化/CultureInfo
Use an overload of
ToString()
instead of aToShortDateString()
method. Supply anIFormatProvider
.This should be helpful in forming a specific date-time string:
http://www.csharp-examples.net/string-format-datetime/
This should be helpful with localization issues:
How do you handle localization / CultureInfo