【发布时间】:2012-08-08 20:52:12
【问题描述】:
这段代码已经运行了很长时间,但是当我尝试将 DateTime.Now 作为 outageEndDate 参数传递时,它现在已经崩溃了:
public Outage(DateTime outageStartDate, DateTime outageEndDate, Dictionary<string, string> weeklyHours, string province, string localProvince)
{
this.outageStartDate = outageStartDate;
this.outageEndDate = outageEndDate;
this.weeklyHours = weeklyHours;
this.province = province;
localTime = TimeZoneInfo.FindSystemTimeZoneById(timeZones[localProvince]);
if (outageStartDate < outageEndDate)
{
TimeZoneInfo remoteTime = TimeZoneInfo.FindSystemTimeZoneById(timeZones[province]);
outageStartDate = TimeZoneInfo.ConvertTime(outageStartDate, localTime, remoteTime);
outageEndDate = TimeZoneInfo.ConvertTime(outageEndDate, localTime, remoteTime);
我在最后一行收到的错误消息是 DateTime 参数 (outageEndDate) 上的 Kind 属性设置不正确。我已经用谷歌搜索并检查了 SO 示例,但我并不真正理解错误消息。
感谢任何建议。
问候。
编辑 - 确切的错误消息是:
The conversion could not be completed because the supplied DateTime did not have the Kind
property set correctly. For example, when the Kind property is DateTimeKind.Local, the source
time zone must be TimeZoneInfo.Local. Parameter name: sourceTimeZone
编辑:outageEndDate.Kind = Utc
【问题讨论】:
-
请发布确切的错误谢谢
-
outageEndDate的 DateTimeKind 是什么?Debug.WriteLine(string.Format("{0}", outageEndDate.Kind)); -
@DJKRAZE:请参阅对 OP 的编辑。
-
@MonroeThomas:请参阅对 OP 的编辑。