DateTime & TimeZone

  • Thread starter Thread starter Benjamin The Donkey
  • Start date Start date
B

Benjamin The Donkey

How do I convert Date to another TimeZone in C#? I am using .NET 1.1
right now.
 
Benjamin said:
How do I convert Date to another TimeZone in C#? I am using .NET 1.1
right now.

Other than doing it explicitly, I'm not aware of a good way. The only
conversion I know of is between UTC and local, according to your current
system settings.

If you know the specific UTC offset for the timezones, it should be a
simple matter to convert from one to the other.

I suppose you could modify the current system settings as part of the
conversion process, but that seems like a pretty ugly solution.

Pete
 
Benjamin said:
How do I convert Date to another TimeZone in C#? I am using .NET 1.1
right now.

3 solutions:

1) wait for .NET 3.5

see
http://msdn2.microsoft.com/en-us/library/system.timezoneinfo_members(VS.90).aspx

2) sniff in registry

code snippet:

public static int GetUTCOffset1(string target)
{
RegistryKey tzs =
Registry.LocalMachine.OpenSubKey("SOFTWARE").OpenSubKey("Microsoft").OpenSubKey("Windows
NT").OpenSubKey("CurrentVersion").OpenSubKey("Time Zones");
foreach(string tzn in tzs.GetSubKeyNames())
{
if(tzn.Contains(target) ||
((string)tzs.OpenSubKey(tzn).GetValue("Display")).Contains(target))
{
return
-BitConverter.ToInt32((byte[])(tzs.OpenSubKey(tzn).GetValue("TZI")), 0);
}
}
throw new ArgumentException("Unknown timezone " + target);
}

3) use the Java support in vjslib

code snippet:

public static int GetUTCOffset2(string target)
{
java.util.TimeZone tz = java.util.TimeZone.getTimeZone(target);
return tz.getRawOffset() / 60000;
}

Arne

PS: There are a ton of additional problem in relation to summer time,
changes to/from summer time and historic dates, but the above should
get you started.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top