How to convert DateTime of type DateTimeKind.Unspecified to DateTime.Kind.Utc in C# (.NET)

后端 未结 3 1630
半阙折子戏
半阙折子戏 2021-01-07 16:18

I\'ve inherited C# code that has an awful lot of DateTimes where the Kind property is DateTimeKind.Unspecified. These are fed into Datetime.ToUniversalTime() which gives ba

3条回答
  •  清酒与你
    2021-01-07 16:44

    Do you maybe need something like this:

    var unspecified = new DateTime(2016, 12, 12, 10, 10, 10, DateTimeKind.Unspecified);
    var specified = DateTime.SpecifyKind(unspecified, DateTimeKind.Utc);
    

    About SpecifyKind() method from MSDN:

    The SpecifyKind method creates a new DateTime object using the specified kind parameter and the original time value.

    It will create new object, new Kind and same time value. You cannot change Kind of existing object, you need to create new one with same values and different Kind.

    Regarding to other question here are supported types in SQL Compact. And here is issue regarding to DateTimeOffset. It looks like that it is not supported yet in Sql Compact.

提交回复
热议问题