Get DateTime For Another Time Zone Regardless of Local Time Zone

C#Datetime.Net 2.0

C# Problem Overview


Regardless of what the user's local time zone is set to, using C# (.NET 2.0) I need to determine the time (DateTime object) in the Eastern time zone.

I know about these methods but there doesn't seem to be an obvious way to get a DateTime object for a different time zone than what the user is in.

 DateTime.Now
 DateTime.UtcNow
 TimeZone.CurrentTimeZone

Of course, the solution needs to be daylight savings time aware.

C# Solutions


Solution 1 - C#

In .NET 3.5, there is TimeZoneInfo, which provides a lot of functionality in this area; 2.0SP1 has DateTimeOffset, but this is much more limited.

Getting UtcNow and adding a fixed offset is part of the job, but isn't DST-aware.

So in 3.5 I think you can do something like:

DateTime eastern = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(
    DateTime.UtcNow, "Eastern Standard Time");

But this simply doesn't exist in 2.0; sorry.

Solution 2 - C#

As everyone else mentioned, .NET 2 doesn't contain any time zone information. The information is stored in the registry, though, and its fairly trivial to write a wrapper class around it:

SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Time Zones

contains sub-keys for all time zones. The TZI field value contains all the transition and bias properties for a time zone, but it's all stuffed in a binary array. The most important bits (bias and daylight), are int32s stored at positions 0 and 8 respectively:

int bias = BitConverter.ToInt32((byte[])tzKey.GetValue("TZI"), 0);
int daylightBias = BitConverter.ToInt32((byte[])tzKey.GetValue("TZI"), 8);

Here's an archive of How to get time zone info (DST) from registry?

Solution 3 - C#

From - http://msdn.microsoft.com/en-us/library/system.timezoneinfo.converttimefromutc.aspx

This allows a time zone to be found by name, in case the US ever floats 15 degrees west or east from the London meridian.

DateTime timeUtc = DateTime.UtcNow;
try
{
   TimeZoneInfo cstZone = TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time");
   DateTime cstTime = TimeZoneInfo.ConvertTimeFromUtc(timeUtc, cstZone);
   Console.WriteLine("The date and time are {0} {1}.", 
                     cstTime, 
                     cstZone.IsDaylightSavingTime(cstTime) ?
                             cstZone.DaylightName : cstZone.StandardName);
}
catch (TimeZoneNotFoundException)
{
   Console.WriteLine("The registry does not define the Central Standard Time zone.");
}                           
catch (InvalidTimeZoneException)
{
   Console.WriteLine("Registry data on the Central STandard Time zone has been corrupted.");
}

Solution 4 - C#

I'll save you the time and tell you that there is no way in .net proper, version 2.0 to get a DateTime object for another time zone different from the one that the software is running on (other than UTC).

However, that doesn't mean there isn't a way to do it outside of .net. Take a look here at the TimeZoneInformation class. This class wraps some p/invoke stuff to the Win O/S to get the time zone information from the O/S. I successfully used it back when 2.0 was new and it worked very well. The site I was working on had to be able to show every date/time local to the user and had to be DST-aware, and this class filled the bill for us.

Solution 5 - C#

How about

 DateTime lclTime = DateTime.Now;
 DateTime ept = lclTime.ToUniversalTime().AddHours(
                   IsEasternDaylightSavingTime(
                       lclTime.ToUniversalTime())? -5: -4)

or if you already have local UTC, just

 DateTime lclUtc = DateTime.UtcNow;
 DateTime ept = lclUtc.AddHours(
                  IsEasternDaylightSavingTime(lclUtc)? -5: -4)

Use static dictionary of hard coded values for Spring-forward and fall back dates for Eastern time for the next 50 years.. That's only 300 bytes or so... and then index into that to determine whether it's Daylight savings time on east coast... As pointed out, you don;t care whether it's dST in local zone or not...

 private static bool IsEasternDaylightSavingTime(DateTime utcDateTime)
   {
        // hard coded method to determine 
        // whether utc datetime is Eastern Standard time
        // or Eastern Daylight Time
   }



        

Solution 6 - C#

A date comparison is a date comparison. DateTime just wraps a memory structure which is defined in ticks and makes methods to access commonly used parts of the memory e.g. day or year or Time or TimeOfDay etc.

Furthermore conversion is only possible if you know both the source snd destination offsets and then the calculation is always as given is given by -1 * (sourceOffset - destOffset)

Where the part in parenthesis represents the time zone difference.

Please also see

https://stackoverflow.com/questions/27415652/get-eastern-time-in-c-sharp-without-converting-local-time

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDaveView Question on Stackoverflow
Solution 1 - C#Marc GravellView Answer on Stackoverflow
Solution 2 - C#AndyView Answer on Stackoverflow
Solution 3 - C#Christopher EdwardsView Answer on Stackoverflow
Solution 4 - C#Robert C. BarthView Answer on Stackoverflow
Solution 5 - C#Charles BretanaView Answer on Stackoverflow
Solution 6 - C#JayView Answer on Stackoverflow