在windows中,我们得到如下所示的时区列表:
ID Time zone name Display string
-- -------------- --------------
0 Dateline Standard Time (UTC-12:00) International Date Line West
110 UTC-11 (UTC-11:00) Coordinated Universal Time -11
200 Hawaiian Standard Time (UTC-10:00) Hawaii
300 Alaskan Standard Time (UTC-09:00) Alaska更多的这里。
我使用此列表从一个时区转换为另一个时区,使用接受上面列表中显示的时区名称的TimeZoneInfo类。
例如。
// Local time zone to UTC
var utcOffset = new DateTimeOffset(DateTime.UtcNow, TimeSpan.Zero);
var localTimeZone = TimeZoneInfo.FindSystemTimeZoneById(timezoneName); // here tz name can be any name from above table
var localOffset = new DateTimeOffset(date.Value, localTimeZone.GetUtcOffset(utcOffset));
DateTime utcDate = localOffset.UtcDateTime;现在,我遇到了SalesForce时区表示形式,如:
Time Zone Code Time Zone Name
-------------- --------------
GMT+14:00 Line Is. Time (Pacific/Kiritimati)
GMT+13:00 Phoenix Is.Time (Pacific/Enderbury)
GMT+13:00 Tonga Time (Pacific/Tongatapu)
GMT+12:45 Chatham Standard Time (Pacific/Chatham)更多的这里。
我找不到内置的功能来使用上表中给出的用于转换的time zone code或time zone name。
发布于 2017-11-29 09:51:08
如果您乐于坚持使用TimeZoneInfo和DateTime/DateTimeOffset,可以使用Matt的TimeZoneConverter库将IANA ID (括号中的部分,例如Pacific/基里形蒂)转换为Windows时区ID。
来自项目页文档的示例
string tz = TZConvert.IanaToWindows("America/New_York");
// Result: "Eastern Standard Time"或者:
TimeZoneInfo tzi = TZConvert.GetTimeZoneInfo("America/New_York");然而,需要注意的是:
无论如何,我个人建议使用我的野田时间库来处理日期/时间,但我承认,如果已经有很多处理DateTime的代码,这可能是不可行的。
https://stackoverflow.com/questions/47549107
复制相似问题