使用指定语言转换 DateTime
Convert DateTime with a specified language
我正在编写一个 MVC 5 互联网应用程序,我想将 DateTime
转换为显示为 LocalTime
我有语言。这是一个 Azure
网站。
这是我的代码:
DateTime dateTimeUtcNow = DateTime.UtcNow;
DateTime convertedDate = DateTime.SpecifyKind(dateTimeUtcNow, DateTimeKind.Utc);
DateTime dateTimeLocalTime = convertedDate.ToLocalTime();
return dateTimeLocalTime.ToString(new CultureInfo("en-NZ"));
以上代码的输出与 return DateTime
in UTC
的输出完全相同,没有指定语言文化。
如何将 DateTime
中的 UTC
转换为我拥有文化语言的当地时间?
提前致谢。
您可以使用 toString
函数的第二个参数并使用您需要的任何 language/culture...
根据 MSDN,您可以使用 "d"
格式代替 ToShortDateString
...
所以基本上像这样 return 作为新西兰英语:
CultureInfo enAU = new CultureInfo("en-NZ");
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;
}
如果您需要根据特定 language/culture...
解析字符串,您可能还需要 to look at the overloaded tryParse
方法
我正在编写一个 MVC 5 互联网应用程序,我想将 DateTime
转换为显示为 LocalTime
我有语言。这是一个 Azure
网站。
这是我的代码:
DateTime dateTimeUtcNow = DateTime.UtcNow;
DateTime convertedDate = DateTime.SpecifyKind(dateTimeUtcNow, DateTimeKind.Utc);
DateTime dateTimeLocalTime = convertedDate.ToLocalTime();
return dateTimeLocalTime.ToString(new CultureInfo("en-NZ"));
以上代码的输出与 return DateTime
in UTC
的输出完全相同,没有指定语言文化。
如何将 DateTime
中的 UTC
转换为我拥有文化语言的当地时间?
提前致谢。
您可以使用 toString
函数的第二个参数并使用您需要的任何 language/culture...
根据 MSDN,您可以使用 "d"
格式代替 ToShortDateString
...
所以基本上像这样 return 作为新西兰英语:
CultureInfo enAU = new CultureInfo("en-NZ");
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;
}
如果您需要根据特定 language/culture...
解析字符串,您可能还需要 to look at the overloadedtryParse
方法