我们会需要把 local 时间转换成某一时区的时间, 这里用.Net 的内置方法来实现:
///UTC 时间
DateTime dt = TimeZoneInfo.ConvertTimeToUtc(DateTime.Now, TimeZoneInfo.Local);
Console.WriteLine(dt.ToString());
DateTime dt2 = DateTime.Now;
Console.WriteLine(dt2.ToString());
//美国加拿大时区
var dt4 = TimeZoneInfo.ConvertTimeFromUtc(dt, TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time"));
Console.WriteLine(dt4.ToString());
//列举所有支持的时区列表
var list = TimeZoneInfo.GetSystemTimeZones();
foreach (TimeZoneInfo tzi in list)
{
Console.WriteLine(tzi.Id);
}
DateTime eastern = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(DateTime.UtcNow, "Eastern Standard Time");
dt.ToUniversalTime();
var currentTimeZone = TimeZone.CurrentTimeZone;
//简单的转换
DateTime currentUSTime = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(DateTime.Now.ToUniversalTime(), "Eastern Standard Time");