我有一个来自数据库记录的 UTC DateTime 值。我还有一个用户指定的时区(TimeZoneInfo 的一个实例)。如何将该 UTC DateTime 转换为用户的本地时区?另外,如何确定用户指定的时区当前是否正在遵守 DST?我正在使用.NET 3.5。
谢谢,马克
最好的方法就是简单地使用TimeZoneInfo.ConvertTimeFromUtc
.
// you said you had these already
DateTime utc = new DateTime(2014, 6, 4, 12, 34, 0);
TimeZoneInfo tzi = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time");
// it's a simple one-liner
DateTime pacific = TimeZoneInfo.ConvertTimeFromUtc(utc, tzi);
唯一的问题是传入的DateTime
值可能没有DateTimeKind.Local
那种类型。它必须是Utc
或Unspecified
。
如果要将 DateTimeOffset 转换为另一个 DateTimeOffset,可以在 TimeZoneInfo 中使用专用函数:
DateTimeOffset newTime = TimeZoneInfo.ConvertTime(
DateTimeOffset.UtcNow,
TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time")
);
看看DateTimeOffset结构:
// user-specified time zone
TimeZoneInfo southPole =
TimeZoneInfo.FindSystemTimeZoneById("Antarctica/South Pole Standard Time");
// an UTC DateTime
DateTime utcTime = new DateTime(2007, 07, 12, 06, 32, 00, DateTimeKind.Utc);
// DateTime with offset
DateTimeOffset dateAndOffset =
new DateTimeOffset(utcTime, southPole.GetUtcOffset(utcTime));
Console.WriteLine(dateAndOffset);
对于 DST,请参阅TimeZoneInfo.IsDaylightSavingTime方法。
bool isDst = southpole.IsDaylightSavingTime(DateTime.UtcNow);
南极洲的答案仅适用于与 UTC 匹配的时区。我对这个DateTimeOffset
功能感到非常痛苦,经过数小时的反复试验,我设法产生了一个适用于所有时区的实用转换扩展功能。
static public class DateTimeFunctions
{
static public DateTimeOffset ConvertUtcTimeToTimeZone(this DateTime dateTime, string toTimeZoneDesc)
{
if (dateTime.Kind != DateTimeKind.Utc) throw new Exception("dateTime needs to have Kind property set to Utc");
var toUtcOffset = TimeZoneInfo.FindSystemTimeZoneById(toTimeZoneDesc).GetUtcOffset(dateTime);
var convertedTime = DateTime.SpecifyKind(dateTime.Add(toUtcOffset), DateTimeKind.Unspecified);
return new DateTimeOffset(convertedTime, toUtcOffset);
}
}
例子:
var currentTimeInPacificTime = DateTime.UtcNow.ConvertUtcTimeToTimeZone("Pacific Standard Time");
// TO get Currrent Time in current Time Zone of your System
var dt = DateTime.Now;
Console.WriteLine(dt);
// Display Time Zone of your System
Console.WriteLine(TimeZoneInfo.Local);
// Convert Current Date Time to UTC Date Time
var utc = TimeZoneInfo.ConvertTimeToUtc(dt, TimeZoneInfo.Local);
Console.WriteLine(utc);
// Convert UTC Time to Current Time Zone
DateTime pacific = TimeZoneInfo.ConvertTimeFromUtc(utc, TimeZoneInfo.Local);
Console.WriteLine(pacific);
Console.ReadLine();
这是另一个问题:如果您在 Linux 服务器上运行代码,则需要使用 Linux 系统作为时区名称。例如,“Central Standard Time”将是“America/Chicago”。tz 列表在这里: https ://en.wikipedia.org/wiki/List_of_tz_database_time_zones
这是 isWindows 开关的示例:
public static class DateTimeHelper
{
public static string ConvertUtcToCst(this string dateTimeString)
{
if (string.IsNullOrWhiteSpace(dateTimeString))
{
return string.Empty;
}
if (DateTime.TryParse(dateTimeString, out DateTime outputDateTime))
{
try
{
var isWindows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
TimeZoneInfo cstZone = null;
if (isWindows)
{
cstZone = TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time");
}
else
{
cstZone = TimeZoneInfo.FindSystemTimeZoneById("America/Chicago");
}
var cstDateTime = TimeZoneInfo.ConvertTimeFromUtc(outputDateTime, cstZone);
return cstDateTime.ToString();
}
catch (TimeZoneNotFoundException)
{
return "The registry does not define the Central Standard Time zone.";
}
catch (InvalidTimeZoneException)
{
return "Registry data on the Central Standard Time zone has been corrupted.";
}
catch (Exception ex)
{
return $"Error :: {ex.Message} :: {ex.ToString()}";
}
}
return string.Empty;
}
}