带有日期时间解析的时区转换时间

时间:2017-07-27 06:16:29

标签: c# datetime

我正在安排一项服务,该服务计划在特定时间运行,如下所示

<add key="ScheduledTime" value="18:00" />

我正在尝试将其转换为PST区域,如下所示

TimeZoneInfo cstZone = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time");
scheduledTime = TimeZoneInfo.ConvertTime(DateTime.Parse("18:00"), cstZone);

但我仍然得到我的系统日期而不是PST可以有人告诉我我做错了以下工作时我有以下代码

DateTime timeUtc = DateTime.UtcNow;
TimeZoneInfo cstZone = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time");
DateTime cstTime = TimeZoneInfo.ConvertTime(timeUtc, cstZone);
Console.WriteLine("The date and time are {0} {1}.", cstTime, cstZone.IsDaylightSavingTime(cstTime) ?                                         cstZone.DaylightName : cstZone.StandardName);

enter image description here

这就是我所拥有的

DateTime timeUtc = DateTime.UtcNow;
TimeZoneInfo cstZone = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time");
 DateTime cstTime = TimeZoneInfo.ConvertTime(timeUtc, cstZone);
Console.WriteLine("The date and time are {0} {1}.",cstTime,
cstZone.IsDaylightSavingTime(cstTime) ? cstZone.DaylightName : cstZone.StandardName);

DateTime cst = TimeZoneInfo.ConvertTime(DateTime.Parse("18:00"), cstZone);
Console.Write(cst);

1 个答案:

答案 0 :(得分:0)

服务的时区可能不是您所期望的。打印并检查:

Console.WriteLine(TimeZoneInfo.Local);