【问题标题】:Is there any function for UTC to convert to particular datetime to UTC date timeUTC 是否有任何功能可以将特定日期时间转换为 UTC 日期时间
【发布时间】:2010-09-22 02:23:42
【问题描述】:

例如,我想将特定日期 12-11-2008 11:33:04.510 转换为 UTC 日期时间。任何人都可以帮助我如何做到这一点。我想在 c# 编码中做到这一点。

【问题讨论】:

    标签: c#


    【解决方案1】:

    只需使用DateTime.ToUniversalTime,假设它目前在您计算机的本地时区。

    【讨论】:

      【解决方案2】:

      DateTime 不支持时区。它可以被视为当地时间或UTC时间,正如Jon Skeet所说,DateTime.ToUniversalTime可以在它们之间转换。

      在 .NET3.5 中还有 TimeZoneInfo 类,它允许在任意时区之间转换 DateTime,但根据您的需要,前者可能已经足够了。 还有一个 DateTimeOffset 类,它的工作方式与 DateTime 类似,只是它还存储与 UTC 的偏移量,如果您必须处理多个时区,它会更加健壮。

      【讨论】:

        【解决方案3】:

        如果您希望将 DateTime 标识为 UTC,还可以为其分配 DateTimeKind,

        DateTime saveNow = DateTime.Now; 日期时间 myDt; myDt = DateTime.SpecifyKind(saveNow, DateTimeKind.Utc);

        或者,如果您知道它是本地的: 字符串 formattedDate = "12-11-2008 11:33:04.510"; DateTime localDt = DateTime.Parse(formattedDate, null, DateTimeStyles.AssumeLocal);

        或者,如果您知道它是 UTC: 字符串 formattedDate = "12-11-2008 11:33:04.510"; DateTime localDt = DateTime.Parse(formattedDate, null, DateTimeStyles.AssumeUniversal);

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2021-12-31
          • 2014-01-24
          • 2012-10-07
          • 1970-01-01
          • 2011-07-11
          • 2014-05-06
          • 1970-01-01
          相关资源
          最近更新 更多