Search code examples
c#datetimeutc

C# DateTime to UTC Time without changing the time


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

Example:

DateTime dateTime = GetSomeDateTime(); // dateTime here is 3pm
dateTime.ToUtcDateTime() // datetime should still be 3pm

Solution

  • 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);