【发布时间】:2016-12-08 03:12:28
【问题描述】:
我正在尝试从数据库中获取数据并使用 C# 将日期时间值转换为 UTC
我试过了,还是不行。
foreach (GrJscMstr gjm in grJscMstrs)
{
gjm.gr_sch_date = DateTime.SpecifyKind(gjm.gr_sch_date, DateTimeKind.Utc);
}
然后,我尝试了这个,它也不起作用:
foreach (GrJscMstr gjm in grJscMstrs)
{
gjm.gr_sch_date = new DateTime(gjm.gr_sch_date.Year, gjm.gr_sch_date.Month, gjm.gr_sch_date.Day, 0, 0, 0, DateTimeKind.Utc);
}
然后我将我的代码修改为运行良好的代码。
foreach (GrJscMstr gjm in grJscMstrs)
{
gjm.gr_sch_date = new DateTime(gjm.gr_sch_date.Year, gjm.gr_sch_date.Month, gjm.gr_sch_date.Day, 0, 0, **1**, DateTimeKind.Utc);
}
我需要知道为什么。这对我来说没有任何意义。 DateTime.SpecifyKind(gjm.gr_sch_date, DateTimeKind.Utc); 应该可以正常工作。
【问题讨论】:
-
gr_sch_date 是什么类型的?
-
gjm.gr_sch_date是本地时区的DateTime值吗?如果是这样,请尝试使用TimeZoneInfo.ConvertTimeToUtc(DateTime.SpecifyKind(gjm.gr_sch_date.Date, DateTimeKind.Local))。如果未指定日期,请改用DateTimeKind.Unspecified。如果您只是将时区重新配置为 UTC,AFAIK,DateTime.SpecifyKind(gjm.gr_sch_date, DateTimeKind.Utc)将在不使用时区差异的情况下转换为 UTC。 -
那些是非常命名的变量和类。
-
@CodingYoshi 日期时间
-
@Kinetic 我知道!但是,遗留代码。在这儿无事可做。 :-(