TimeZoneInfo.ConvertTimeFromUtc returned wrong DateTime

a 夏天 提交于 2021-02-19 07:47:06

问题


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

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