C# DateTime to UTC Time without changing the time

前端 未结 4 1557
渐次进展
渐次进展 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:43

    6/1/2011 4:08:40 PM Local
    6/1/2011 4:08:40 PM Utc
    

    from

    DateTime dt = DateTime.Now;            
    Console.WriteLine("{0} {1}", dt, dt.Kind);
    DateTime ut = DateTime.SpecifyKind(dt, DateTimeKind.Utc);
    Console.WriteLine("{0} {1}", ut, ut.Kind);
    

提交回复
热议问题