Convert a time from one time zone to another time zone

I need to convert a particular date from one time to another time zone in c# 2.0 (i.e. visual studion 2005). There is a class called Timezoneinfo in c# 3.0 (i.e. visual studio 2008) to convert date from one time zone to another time zone.

can any body help me to convert a particular date from one time to another time zone using c# 2.0
LVL 1
ZoniacAsked:
Who is Participating?
 
lazyberezovskyConnect With a Mentor Commented:
List of time zones: TimeZoneInfo.GetSystemTimeZones())

Converting time:

string tokyoTimeZoneId = "Tokyo Standard Time";
string centralEuropeTimeZoneId = "Central Europe Standard Time";
DateTime europeTime = new DateTime(2010, 4, 21, 15, 0, 0);
DateTime tokyoTime = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(europeTime, centralEuropeTimeZoneId, tokyoTimeZoneId);

Open in new window

0
 
lucky_jamesCommented:
you can use the timezone class itself.
http://msdn.microsoft.com/en-us/library/system.timezone.aspx

Let me know if you are stuck somewhere.
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
ZoniacAuthor Commented:
Hi All,

I'm looking for a solution in C#2.0.

TimeZoneInfo.ConvertTimeBySystemTimeZoneId(..) is a method available in C#3.0 but not in C#2.0.

Is there any equivalent or alternative class/method for the same in C#2.0?
0
 
ArkCommented:
You should read these info from registry SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones\"
To covert from one zone to another use:

[DllImport("kernel32.dll", CharSet = CharSet.Auto, ExactSpelling = false)]
private static extern int SystemTimeToTzSpecificLocalTime(ref
TIME_ZONE_INFORMATION lpTimeZone, ref SYSTEMTIME lpUniversalTIme, out
SYSTEMTIME lpLocalTime);

[DllImport("kernel32.dll", CharSet = CharSet.Auto, ExactSpelling = false)]
private static extern int TzSpecificLocalTimeToSystemTime(ref
TIME_ZONE_INFORMATION lpTimeZone, ref SYSTEMTIME lpLocalTime, out SYSTEMTIME
lpUniversalTIme);

[StructLayout(LayoutKind.Sequential)]
private struct SYSTEMTIME
{
public ushort wYear;
public ushort wMonth;
public ushort wDayOfWeek;
public ushort wDay;
public ushort wHour;
public ushort wMinute;
public ushort wSecond;
public ushort wMilliseconds;
}

//Registry time zone format. See KB article Q115231
[StructLayout(LayoutKind.Sequential)]
private struct REG_TIME_ZONE_INFORMATION
{
public int Bias;
public int StandardBias;
public int DaylightBias;
public SYSTEMTIME StandardDate;
public SYSTEMTIME DaylightDate;
}

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
private struct TIME_ZONE_INFORMATION
{
public int Bias;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
public string StandardName;
public SYSTEMTIME StandardDate;
public int StandardBias;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
public string DaylightName;
public SYSTEMTIME DaylightDate;
public int DaylightBias;
}

/To get TimeZone's list:
private static List<TIME_ZONE_INFORMATION> GetTimeZones()
{
List<TIME_ZONE_INFORMATION> list = new List<TIME_ZONE_INFORMATION>();
RegistryKey key =
Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows
NT\CurrentVersion\Time Zones");
if (key == null)
return list;

string[] subKeyNames = key.GetSubKeyNames();
foreach (string subKeyName in subKeyNames)
{
RegistryKey subKey = key.OpenSubKey(subKeyName);
if (subKey != null)
{
object value = subKey.GetValue("TZI");
if (value != null)
{
int length =
Marshal.SizeOf(typeof(REG_TIME_ZONE_INFORMATION));
IntPtr p = Marshal.AllocHGlobal(length);
Marshal.Copy((byte[])value, 0, p, length);
REG_TIME_ZONE_INFORMATION rtzi =
(REG_TIME_ZONE_INFORMATION)Marshal.PtrToStructure(p,
typeof(REG_TIME_ZONE_INFORMATION));
Marshal.FreeHGlobal(p);

TIME_ZONE_INFORMATION tzi = new TIME_ZONE_INFORMATION();
tzi.Bias = rtzi.Bias;
tzi.DaylightBias = rtzi.DaylightBias;
tzi.StandardBias = rtzi.StandardBias;
tzi.DaylightDate = rtzi.DaylightDate;
tzi.StandardDate = rtzi.StandardDate;
tzi.DaylightName = (string)subKey.GetValue("Dlt", "");
tzi.StandardName = (string)subKey.GetValue("Std", "");
list.Add(tzi);
}
subKey.Close();
}
}
key.Close();
return list;
}

Open in new window

0
 
ZoniacAuthor Commented:
The solution is c# 3.0 but it is quiet helpfull
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.