net convert localtime to utc free download for windows 10 enterprise 64bit

UTC to Local Time and Back Again

Update: See Part 2 of this series for details about converting to and from arbitrary time zones

What’s UTC, and Why Should I Care?

Application developers often rely on UTC (Coordinated Universal Time) to store DateTimes in a location-neutral and time zone-neutral form.  But sometimes you have the wrong kind of DateTime and need to convert it before you can use it.

Say, for example, you wanted to develop an application for users spread around the United States, and you wanted to stamp each record they change with a last-updated time stamp.  If you use DateTime.Now on the client application to generate these time stamps, it will become impossible to compare changes made by two or more users from different time zones. 

To solve this problem, most enterprise .NET applications use DateTime.UtcNow to get the current UTC time and store that in the database,  However, it would be pretty lousy to display UTC times in the user interface expecting your users to always make the mental shift in their heads.

To enable easy conversion of UTC times to local times, Microsoft has provided several useful methods on the DateTime class.  First, though, we need to familiarize ourselves with the DateTime.Kind property.

DateTime.Kind & DateTime.SpecifyKind()

By default, a DateTime object’s Kind is Unspecified.  If you use DateTime.Now or DateTime.UtcNow properties, the Kind of the objects returned will be Local and Utc, respectively.  This is as you’d expect, but keep in mind that a DateTime created using its constructor or converted from a String will typically be Unspecified.

DateTimes created using a DateTime constructor without a DateTmeKind will be Unspecified

DateTime utcTime = DateTime .UtcNow;

DateTimes created using DateTime.Now or UtcNow have their Kind set automatically

DateTime.Kind is not settable, so if you want to change the Kind of an existing DateTime object, you must do so using DateTime.SpecifyKind().

DateTime specifiedTime = DateTime .SpecifyKind(unspecifiedTime, DateTimeKind .Local);

Manually setting the Kind of a DateTime using DateTime.SpecifyKind()

DateTime.ToLocalTime()

If you want to convert a DateTime to local time you can use its ToLocalTime() method.  The result, though, will depend on the Kind specified for the object.  Since you’re asking .NET to convert to local time, unspecified DateTimes will be treated as Utc.  Calling ToLocalTime on a local DateTime will have no effect, but calling the method on a UTC DateTime will convert it from UTC to local time, as you would expect.

DateTime localTime = new DateTime (2010, 6, 1, 12, 0, 0, DateTimeKind .Local);

DateTime utcTime = new DateTime (2010, 6, 1, 12, 0, 0, DateTimeKind .Utc);

//For me, in EDT, outputs "6/1/2010 8:00:00 AM"

//Unspecified, assumes Utc

DateTime unspecifiedTime = new DateTime (2010, 6, 1, 12, 0, 0);

The conversion result depends on the Kind specified for the DateTime

Warning!   Windows XP only supports having one daylight savings adjustment rule.  When the daylight savings start and end dates were updated in 2007 by the U.S. Congress, Microsoft applied a new conversion rule to Windows.  The result is that for XP systems, some dates in 2007 and before may not convert properly between UTC and local time.  Newer Windows versions do not have this problem.  More information about this in my next blog post.

DateTime.ToUniversalTime()

If you want to convert a DateTime to UTC time you can use its ToUniversalTime() method.  As with ToLocalTime(), the result depends on the Kind specified for the object.  Since you’re asking .NET to convert to UTC time, unspecified DateTimes will be treated as Local.  Notice that that is the opposite of how they are treated when calling ToLocalTime() on an Unspecified DateTime.  This is where using Unspecified DateTimes can get dangerous.

Of course, calling ToUniversalTime on a Utc DateTime will have no effect, but calling the method on a Local DateTime will convert it from local time to UTC, as you would expect.

DateTime localTime = new DateTime (2010, 6, 1, 12, 0, 0, DateTimeKind .Local);

DateTime utcTime = new DateTime (2010, 6, 1, 12, 0, 0, DateTimeKind .Utc);

//For me, in EDT, outputs "6/1/2010 12:00:00 PM"

//Unspecified, assumes Local

DateTime unspecifiedTime = new DateTime (2010, 6, 1, 12, 0, 0);

The conversion result depends on the Kind specified for the DateTime, and Unspecified can be inconsistent

Hopefully that helps clear up simple UTC-to-Local and Local-to-UTC time conversions.  Stay tuned for my next blog post, where I’ll cover conversions between other time zones.