无法将DateTime值转换为UTC

时间:2016-12-08 03:12:28

标签: c# json datetime utc

我正在尝试从数据库中获取数据,并使用C#

将datetime值转换为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);应该正常工作。

1 个答案:

答案 0 :(得分:0)

Lonely Planeteer没有指明"没有工作"手段。下面的示例演示了如何通过使用Lonely Planeteer所需的语法来松散地改变DateTime的类型。也许这个完整的工作示例将允许Lonely Planeteer找到问题。

using System;

namespace StOv3
{
    class Program
    {
        static void Main(string[] args)
        {
            DateTime unspecifiedTime = new DateTime(2016, 12, 8, 0, 0, 0, DateTimeKind.Unspecified);
            DateTime localTime = new DateTime(2016, 12, 8, 0, 0, 0, DateTimeKind.Local);
            DateTime utcTime = new DateTime(2016, 12, 8, 0, 0, 0, DateTimeKind.Utc);
            Console.WriteLine("A DateTime, unspecified whether UTC or local {0}, Kind {1}", unspecifiedTime, unspecifiedTime.Kind);
            Console.WriteLine("A local DateTime {0}, Kind {1}", localTime, localTime.Kind);
            Console.WriteLine("A UTC DateTime {0}, Kind {1}", utcTime, utcTime.Kind);
            // Attempt to redefine variables with same values;

            unspecifiedTime = new DateTime(2016, 12, 8, 0, 0, 0, DateTimeKind.Unspecified);
            localTime = new DateTime(2016, 12, 8, 0, 0, 0, DateTimeKind.Local);
            utcTime = new DateTime(2016, 12, 8, 0, 0, 0, DateTimeKind.Utc);
            Console.WriteLine("A DateTime, unspecified whether UTC or local {0}, Kind {1}", unspecifiedTime, unspecifiedTime.Kind);
            Console.WriteLine("A local DateTime {0}, Kind {1}", localTime, localTime.Kind);
            Console.WriteLine("A UTC DateTime {0}, Kind {1}", utcTime, utcTime.Kind);

            // Attempt to change kind of variable

            unspecifiedTime = DateTime.SpecifyKind(unspecifiedTime, DateTimeKind.Utc);
            localTime = DateTime.SpecifyKind(localTime, DateTimeKind.Unspecified);
            utcTime = DateTime.SpecifyKind(utcTime, DateTimeKind.Utc);
            Console.WriteLine("A DateTime, originally unspecified whether UTC or local {0}, Kind {1}",
                unspecifiedTime, unspecifiedTime.Kind);
            Console.WriteLine("A DateTime, originally local {0}, Kind {1}", localTime, localTime.Kind);
            Console.WriteLine("A DateTime, originally UTC {0}, Kind {1}", utcTime, utcTime.Kind);

        }
    }
}

控制台输出:

A DateTime, unspecified whether UTC or local 12/8/2016 00:00:00, Kind Unspecified
A local DateTime 12/8/2016 00:00:00, Kind Local
A UTC DateTime 12/8/2016 00:00:00, Kind Utc
A DateTime, unspecified whether UTC or local 12/8/2016 00:00:00, Kind Unspecified
A local DateTime 12/8/2016 00:00:00, Kind Local
A UTC DateTime 12/8/2016 00:00:00, Kind Utc
A DateTime, originally unspecified whether UTC or local 12/8/2016 00:00:00, Kind Utc
A DateTime, originally local 12/8/2016 00:00:00, Kind Unspecified
A DateTime, originally UTC 12/8/2016 00:00:00, Kind Utc