C# DateTime to UTC Time without changing the time

前端 未结 4 1545
渐次进展
渐次进展 2020-12-25 09:33

How would I convert a preexisting datetime to UTC time without changing the actual time.

Example:

DateTime dateTime = GetSomeDateTime(); // dateTime          


        
4条回答
  •  离开以前
    2020-12-25 09:50

    You can use the overloaded constructor of DateTime:

    DateTime utcDateTime = new DateTime(dateTime.Year, dateTime.Month, dateTime.Day, dateTime.Hour, dateTime.Minute, dateTime.Second, DateTimeKind.Utc);
    

提交回复
热议问题