【发布时间】:2016-06-23 09:08:46
【问题描述】:
我正在尝试使用此代码转换为瑞典时区:
Thread.CurrentThread.CurrentCulture = new CultureInfo("sv-SE");
TimeZoneInfo cet = TimeZoneInfo.FindSystemTimeZoneById("Central European Standard Time");
DateTime currentDate = DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Local);
var swedishTime = TimeZoneInfo.ConvertTime(currentDate, cet, TimeZoneInfo.Local);
由于某种原因,我得到:
{"转换无法完成,因为提供的 DateTime 没有正确设置 Kind 属性。例如,当 Kind 属性为 DateTimeKind.Local,源时区必须为 TimeZoneInfo.Local。\r\n参数名称:sourceTimeZone"}
我错过了什么?
【问题讨论】:
-
使用正确的类型,DateTimeOffset。单独使用
DateTime无法避免转换错误,即使它设置为DateTimeKind.UTC。例如本地 - 到什么? Web 应用程序、服务器还是浏览器?
标签: c# asp.net-mvc datetime timezone