我需要创建一个具有设定日期和时间的日期时间,该日期和时间将位于特定时区(西亚标准时间、西欧标准时间等)。
必须保留 DST,因此偏移量将被排除,因为对于给定时区,一年中的一半时间为 +2 小时,另一半时间为 +3 小时。
然后我想将日期转换为 UTC。
我尝试这样做,以便稍后可以添加此时区偏移量。然而,首先我不喜欢这个解决方案,我担心每年两次时间改变时我会损失一个小时,其次我收到一个错误:
“Utc DateTime 实例的 UTC 偏移量必须为 0。”
var testTime = new DateTime(testDate.Year, testDate.Month, testDate.Day, 4, 0, 0, DateTimeKind.Utc);
var timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById("West Asia Standard Time");
var timezoneTime = TimeZoneInfo.ConvertTimeFromUtc(runTime, timeZoneInfo);
var offset = timeZoneInfo.GetUtcOffset(timezoneTime);
Run Code Online (Sandbox Code Playgroud)
我想要得到这样的代码
var timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById("West Asia Standard Time");
var testTime = new DateTime(testDate.Year, testDate.Month, testDate.Day, 4, 0, 0, timeZoneInfo);
var utcTime = testTime.ToUniversalTime();
Run Code Online (Sandbox Code Playgroud)
所以,总而言之,我想要有一种方法,我可以在其中传递年、月、日、小时、分钟和时区,作为回报,我将得到 UTC 中的 DateTime 在 javascript 中,有一些库,其中给出了给定的时区作为参数,但我必须在服务器端执行此操作。