【问题标题】:Converting multiple timezone to local time将多个时区转换为本地时间
【发布时间】:2013-01-21 12:52:17
【问题描述】:

我在 CST 时区保存日期时间,如何将 CST 日期时间更改为本地时间。

例如:

在数据库中, 日期时间为 2013-01-21 06:50:00,时区为 CST。此日期时间应转换为当地当前时间。

【问题讨论】:

  • 你关心夏令时吗?
  • CST 不是时区,它是一个缩写,而且是模棱两可的。是中国标准时间吗?古巴标准时间?中部标准时间(美国)、中部标准时间(澳大利亚)等。我希望您在数据库中存储的不仅仅是“CST”。有关缩写的完整列表,请参阅 here

标签: c# datetime timezone


【解决方案1】:

将它们保存为 UTC 时间,然后在加载到 UI 时将它们转换为本地时间。

【讨论】:

    【解决方案2】:

    示例代码如下所示

    using System;
    
    public class Example
    {
     public static void Main()
     {
      DateTime date1 = new DateTime(2010, 3, 14, 2, 30, 0, DateTimeKind.Local);
      Console.WriteLine("Invalid time: {0}", 
                        TimeZoneInfo.Local.IsInvalidTime(date1));
      DateTime utcDate1 = date1.ToUniversalTime();
      DateTime date2 = utcDate1.ToLocalTime();
      Console.WriteLine("{0} --> {1}", date1, date2);      
      }
     }
    // The example displays the following output: 
    //       Invalid time: True 
    //   3/14/2010 2:30:00 AM --> 3/14/2010 3:30:00 AM
    

    希望对你有帮助

    【讨论】:

    • 在此示例中,您在哪里考虑到 OP 指定日期存储在 CST 中?
    猜你喜欢
    • 1970-01-01
    • 2019-05-31
    • 2011-07-13
    • 1970-01-01
    • 1970-01-01
    • 2014-09-04
    • 2019-03-09
    • 2018-01-17
    • 2014-10-05
    相关资源
    最近更新 更多