Convert PST to GMT Irrespective of Server Timezone with .Net DateTime

不羁的心 提交于 2019-12-11 04:58:45

问题


Given a date and time structured as:

"2011-10-21 14:44:19"

which is known to be Pacific Time (adjusted for Daylight Savings when appropriate), I need to parse the DateTime and convert it to UTC, irrespective of the current server time.

I have seen this very similar question that starts with the assumption that the original DateTime is in UTC:

How to format a datetime to GMT irrespective of regional settings?

I'm trying to apply that to my situation, but don't understand how to begin with a Pacific Time (PST or PDT as appropriate) string and arrive at a UTC DateTime. Specifically, that solution applies the flag DateTimeStyles.AssumeUniversal, whereas I need to assume Pacific Time.


回答1:


You are going to have to use TimeZoneInfo.ConvertTimeToUtc(DateTime, TimeZoneInfo) to make the conversion. The server's timezone doesn't matter. Use FindSystemTimeZoneById() to select the timezone you pass as the second argument. You'll want "Pacific Standard Time" for the id.

This function uses a timezone database that's stored in the registry and maintained by Windows. Timezone rules change over time, particularly daylight savings transitions, so keeping Windows Update active on that machine is important to allow Microsoft to update the database. Clearly knowing exactly where the string came from is important for this to work correctly at all.



来源:https://stackoverflow.com/questions/10047541/convert-pst-to-gmt-irrespective-of-server-timezone-with-net-datetime

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!