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

后端 未结 3 1651
半阙折子戏
半阙折子戏 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:50

    You could combine the two above answers for a cleaner solution...

    public static DateTime SetKind(this DateTime DT, DateTimeKind DTKind)
    {        
        return DateTime.SpecifyKind(DT, DTKind);
    }
    

提交回复
热议问题