【问题标题】:UTC to locatime with daylight savingUTC 到夏令时的当地时间
【发布时间】:2020-01-13 10:19:17
【问题描述】:

我们在我们的数据库中以 UTC 格式保存所有日期,并为每个用户保存偏移量。

我们正在通过以下函数转换为本地时间:

 Private Function ConvertToLocalDateTime(ByVal utcOffset As Integer?, ByVal DateToConvert As DateTime?) As DateTime?

        getdatetime = DateToConvert.Value.AddHours(utcOffset)

        If getdatetime.IsDaylightSavingTime Then
            getdatetime.AddHours(-1)
        End If

        Return getdatetime
    End Function

但日期仍然有 1 或 2 小时的时间。

我们如何才能从 utcoffset 正确转换为本地时间,同时兼顾夏令时?

【问题讨论】:

  • docs.microsoft.com/en-us/dotnet/api/…。 *转换还考虑了适用于当前 DateTime 对象表示的时间的夏令时规则。 *
  • 我不清楚您为什么将 UTC 偏移量作为参数。相反,您应该使用 TimeZoneInfo,因为它知道适用于任何情况的时区规则。如果您只记录了用户在特定时间点的 UTC 偏移量,那么您没有足够的信息。

标签: c# utc datetimeoffset gmt


【解决方案1】:

只要将DateToConvert 上的Kind 属性正确设置为Utc,就可以直接调用DateToConvert.ToLocalTime()

【讨论】:

    猜你喜欢
    • 2021-01-21
    • 2016-02-06
    • 2012-04-07
    • 1970-01-01
    • 2014-06-11
    • 1970-01-01
    • 2014-11-30
    • 2012-12-03
    • 2018-03-04
    相关资源
    最近更新 更多