简体   繁体   English

UTC日期时间到本地和返回时间不正确

[英]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. 我需要能够将UTC时间转换为本地时间,增加一小时,并希望转换回UTC进行比较。 However, the basic to TimeZone and back to UTC gives incorrect time. 但是,TimeZone和回到UTC的基本时间不正确。

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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