问题
I called method:
TimeZoneInfo.ConvertTimeFromUtc(DateTime.Now.ToUniversalTime(), TimeZoneInfo.FindSystemTimeZoneById("GMT Standard Time")) // UTC+0
It is returned DateTime for one hour larger than the correct DateTime. Why? How will it fix?
Returned value should be equal DateTime.Now.ToUniversalTime()
回答1:
UTC is equal to GMT. But currently we're in BST due to summer, which is GMT + 1. GMT Standard Time
automatically adjusts for daylight savings. Use Greenwich Standard Time
, rather than GMT Standard Time
if you don't want to adjust for daylight savings.
EDIT: All you have to do is change GMT Standard Time
to Greenwich Standard Time
(See below)
TimeZoneInfo.ConvertTimeFromUtc(DateTime.Now.ToUniversalTime(), TimeZoneInfo.FindSystemTimeZoneById("Greenwich Standard Time"))
回答2:
DateTime.Now.ToUniversalTime()
already retuns UTC, you do not need to convert that again to UTC.
Hint: Use DateTime.UtcNow
if you really just need UTC, then you don't have to care about time zones at all and what your local time zone may be.
Update: Oh, and "GMT standard Time" is not "Greenwich mean time".
来源:https://stackoverflow.com/questions/51326079/timezoneinfo-converttimefromutc-returned-wrong-datetime