简体   繁体   中英

UTC DateTime to Local and Back gives incorrect time

I need to be able to convert UTC time to Local Time, add one hour and would like to convert back to UTC for comparison. However, the basic to TimeZone and back to UTC gives incorrect time.

DateTime utcDateTime = DateTime.SpecifyKind(DateTime.UtcNow, DateTimeKind.Utc);
var localTime = utcDateTime.ToZone("Central Time (US & Canada)"); // CORRECT TIME

// INCORRECT when converted back to utc. It should be the same as utcDateTime
var convertedBack = TimeZoneInfo.ConvertTimeToUtc(localTime); 

在此处输入图片说明

What am I missing?

// This works
TimeZoneInfo tz = TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time");
var convertedBack =TimeZoneInfo.ConvertTimeToUtc(localTime, tz);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM