首页 > 解决方案 > UTC 到本地时区之间的 C# DateTime 转换

问题描述

  1. 能够使用以下代码将本地日期时间转换为 UTC。
    a. TimeZoneInfo.ConvertTimeToUtc(dateTimeValue, TimeZoneInfo.FindSystemTimeZoneById(localTimeZone));
    b. TimeZoneInfo.ConvertTime(dateTimeValue, TimeZoneInfo.FindSystemTimeZoneById(localTimeZone), TimeZoneInfo.FindSystemTimeZoneById("UTC");

  2. 并使用以下代码将 UTC 转换为本地。 a. TimeZoneInfo.ConvertTimeFromUtc(dateTimeValue, TimeZoneInfo.FindSystemTimeZoneById(localTimeZone)); b. TimeZoneInfo.ConvertTime(dateTimeValue, TimeZoneInfo.FindSystemTimeZoneById("UTC"), TimeZoneInfo.FindSystemTimeZoneById(localTimeZone));

当应用程序在 Visual Studio(调试模式)中运行时,它工作正常。但是在美国服务器(东部时区)托管应用程序并从印度浏览应用程序后,从 UTC 到本地的时间转换不起作用。这里 localTimeZone 是“印度标准时间”(本地时区从客户端机器获取并从日期时间转换发送到服务器。)

示例: IST DateTime: 12/10/2018 18:37
UTC DateTime: 2018-12-10 06:05 (IST 到 UTC)
转换的日期时间: 12/11/2018 05:07(UTC 到 IST)

如果我们将本地时区设置为“东部时区”,则转换正确。
任何人都可以为这种情况提供解决方案。

标签: datetime

解决方案


推荐阅读