我想将日期时间从一个时区转换为另一个时区。为此,我需要将区域ID传递给方法FindSystemTimeZoneById。但我没有这些信息,需要通过使用开关盒来确定。
这里我还需要考虑夏令时。但为了确定一个时间是否在夏令时,我需要预先设置防区ID。
有没有什么方法可以确定一个时间是否在没有区域ID的DST中。我的服务器是在1区,我想向区2考虑到夏令时从一个时区转换到另一个时区
这里的时间转换为片段:
public DateTime ConvertToDestTime(DateTime currentTime, string sourceTimeZoneUtc, string serverTimeZoneUtc)
{
TimeZoneInfo sourceTimeZone = TimeZoneInfo.FindSystemTimeZoneById(ReturnTimeZoneString(sourceTimeZoneUtc));
TimeZoneInfo serverTimeZone = TimeZoneInfo.FindSystemTimeZoneById(ReturnTimeZoneString(serverTimeZoneUtc));
DateTime serverTime = TimeZoneInfo.ConvertTime(currentTime, sourceTimeZone, serverTimeZone);
return serverTime;
}
private string ReturnTimeZone(string utcOffset)
{
string timezone = string.Empty;
string isDaylight = //need to determine whether time is in DST here
if (isDaylight == "N")
{
switch (utcOffset)
{
case "-04:00":
timezone = "Atlantic Standard Time";
break;
case "-05:00":
timezone = "Eastern Standard Time";
break;
}
}
else
{
switch (utcOffset)
{
case "-04:00":
timezone = "Eastern Standard Time";
break;
case "-05:00":
timezone = "Central America Standard Time";
break;
}
}
return timezone;
''currentTime'你有什么样的价值? (它的'Kind'属性返回什么?) –
它是作为“未指定”而来的。该字段的值为“7/13/2014 12:00:00 AM” – Priyanka
然后TimeZoneInfo应该只为您处理DST。虽然指定一个时间zonr作为UTC偏移通常是一个非常糟糕的主意。 –