【发布时间】: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