Datetime 使用指定的语言转换日期时间
我正在编写一个MVC5互联网应用程序,我想将Datetime 使用指定的语言转换日期时间,datetime,asp.net-mvc-5,utc,cultureinfo,localtime,Datetime,Asp.net Mvc 5,Utc,Cultureinfo,Localtime,我正在编写一个MVC5互联网应用程序,我想将DateTime转换为LocalTime,在我有这种语言的地方显示出来。这是针对Azure网站的 这是我的密码: DateTime dateTimeUtcNow = DateTime.UtcNow; DateTime convertedDate = DateTime.SpecifyKind(dateTimeUtcNow, DateTimeKind.Utc); DateTime dateTimeLocalTime = convertedDate.ToLo
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"));
上述代码的输出与我在UTC
中返回的DateTime
的输出完全相同,没有指定语言区域性
如何将UTC
中的DateTime
转换为具有区域性语言的本地时间
提前感谢。您可以使用
toString
函数的第二个参数,并使用您需要的任何语言/文化
根据MSDN,您可以使用“d”
格式而不是ToSortDateString
基本上是这样的,以NewZ ealand English的形式返回:
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;
}
如果需要针对特定语言/区域性解析字符串,您可能还需要tryParse
方法