【问题标题】:Converting DateTime.Now To A Different Time Zone将 DateTime.Now 转换为不同的时区
【发布时间】:2012-08-08 20:52:12
【问题描述】:

这段代码已经运行了很长时间,但是当我尝试将 DateTime.Now 作为 outageEndDate 参数传递时,它现在已经崩溃了:

public Outage(DateTime outageStartDate, DateTime outageEndDate, Dictionary<string, string> weeklyHours, string province, string localProvince)
    {
        this.outageStartDate = outageStartDate;
        this.outageEndDate = outageEndDate;
        this.weeklyHours = weeklyHours;
        this.province = province;
        localTime = TimeZoneInfo.FindSystemTimeZoneById(timeZones[localProvince]);

        if (outageStartDate < outageEndDate)
        {
            TimeZoneInfo remoteTime = TimeZoneInfo.FindSystemTimeZoneById(timeZones[province]);
            outageStartDate = TimeZoneInfo.ConvertTime(outageStartDate, localTime, remoteTime);
            outageEndDate = TimeZoneInfo.ConvertTime(outageEndDate, localTime, remoteTime);

我在最后一行收到的错误消息是 DateTime 参数 (outageEndDate) 上的 Kind 属性设置不正确。我已经用谷歌搜索并检查了 SO 示例,但我并不真正理解错误消息。

感谢任何建议。

问候。

编辑 - 确切的错误消息是:

The conversion could not be completed because the supplied DateTime did not have the Kind
property set correctly.  For example, when the Kind property is DateTimeKind.Local, the source
time zone must be TimeZoneInfo.Local.  Parameter name: sourceTimeZone

编辑:outageEndDate.Kind = Utc

【问题讨论】:

  • 请发布确切的错误谢谢
  • outageEndDate 的 DateTimeKind 是什么? Debug.WriteLine(string.Format("{0}", outageEndDate.Kind));
  • @DJKRAZE:请参阅对 OP 的编辑。
  • @MonroeThomas:请参阅对 OP 的编辑。

标签: c# datetime


【解决方案1】:

感谢您澄清您的问题。

如果 DateTime 实例 KindLocal,那么 TimeZoneInfo.ConvertTime 将期望第二个参数是您计算机的本地时区。

如果 DateTime 实例 KindUtc,那么 TimeZoneInfo.ConvertTime 将期望第二个参数是 Utc 时区。

您需要先将 outageEndDate 转换为正确的时区,以防 localProvice 时区与您计算机上的时区不匹配。

outageEndDate = TimeZoneInfo.ConvertTime(outageEndDate, localTime);

【讨论】:

    【解决方案2】:

    这里有一个你可以尝试的例子

    这取决于您所说的“GMT + 1 时区”是什么意思。您是指永久 UTC+1,还是根据 DST 表示 UTC+1 或 UTC+2?

    如果您使用的是 .NET 3.5,请使用 TimeZoneInfo 获取适当的时区,然后使用:

    // Store this statically somewhere
    TimeZoneInfo maltaTimeZone = TimeZoneInfo.FindSystemTimeZoneById("...");
    DateTime utc = DateTime.UtcNow;
    DateTime malta = TimeZoneInfo.ConvertTimeFromUtc(utc, maltaTimeZone );
    

    您需要计算出马耳他时区的系统 ID,但您可以通过在本地运行此代码轻松完成:

    Console.WriteLine(TimeZoneInfo.Local.Id);
    

    如果您使用 .NET 3.5,则需要自己计算夏令时。老实说,最简单的方法就是一个简单的查找表。计算出未来几年的 DST 变化,然后编写一个简单的方法来返回特定 UTC 时间的偏移量,并对该列表进行硬编码。您可能只想要一个已排序的List&lt;DateTime&gt;,其中包含已知更改,并在 1 到 2 小时之间交替,直到您的日期在最后一次更改之后:

    // Be very careful when building this list, and make sure they're UTC times!
    private static readonly IEnumerable<DateTime> DstChanges = ...;
    
    static DateTime ConvertToLocalTime(DateTime utc)
    {
        int hours = 1; // Or 2, depending on the first entry in your list
        foreach (DateTime dstChange in DstChanges)
        {
            if (utc < dstChange)
            {
                return DateTime.SpecifyKind(utc.AddHours(hours), DateTimeKind.Local);
            }
            hours = 3 - hours; // Alternate between 1 and 2
        }
        throw new ArgumentOutOfRangeException("I don't have enough DST data!");
    }
    

    【讨论】:

      猜你喜欢
      • 2011-05-05
      • 1970-01-01
      • 2014-03-23
      • 1970-01-01
      • 2016-08-07
      • 1970-01-01
      • 2019-06-15
      • 2020-10-17
      • 1970-01-01
      相关资源
      最近更新 更多