TimeZone : Get the date and time as per time zone

//Get the Collection of System’s Time Zones.
System.Collections.ObjectModel.ReadOnlyCollection timeZone = TimeZoneInfo.GetSystemTimeZones();
//Convert time zone collection to LIST
List tz = timeZone.ToList();

string strZone=””;
try
{
strZone = tz.AsEnumerable().Where(u => u.DisplayName == “(UTC+01:00) West Central Africa”).SingleOrDefault().StandardName.ToString();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message.ToString());
}

TimeZoneInfo timeZone1 = TimeZoneInfo.Local;
TimeZoneInfo timeZone2 = TimeZoneInfo.FindSystemTimeZoneById(strZone.ToString());

Console.WriteLine(“================= Time Zone =============”);
Console.WriteLine(timeZone1.DisplayName);
Console.WriteLine(timeZone2.DisplayName);
DateTime dtNow = DateTime.Now;
DateTime dtOperationDate = DateTime.Now.AddHours(-1);
Console.WriteLine(“========================================”);

Console.WriteLine(“Local DateTime = ” + dtNow);
Console.WriteLine(“Local Operation Date = ” + dtOperationDate);

try
{
DateTime convertDate = TimeZoneInfo.ConvertTime(dtNow, timeZone1, timeZone2);
DateTime convertOperationDate = TimeZoneInfo.ConvertTime(dtOperationDate, timeZone1, timeZone2);
Console.WriteLine(“Converted DateTime = ” + convertDate.ToString());
Console.WriteLine(“Converted Operation Date = ” + convertOperationDate.ToString());

Console.WriteLine(“Subracted date time = ” + convertDate.Subtract(convertOperationDate).Hours
+ “:” + convertDate.Subtract(convertOperationDate).Minutes + ” hrs ago”);
}
catch(Exception ex)
{
Console.WriteLine( ex.Message.ToString());
}
}
}
}

For More information see following links:

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

http://msdn.microsoft.com/en-us/library/8z6watww.aspx

Share