2012-08-27

How to get date and time in a different Time Zone

To get the Date and time for a different time zone, first we need to create a TimeZoneInfo object from the required time zone, as below

TimeZoneInfo timezone = TimeZoneInfo.FindSystemTimeZoneById("Central Time zone");

Here I’m passing the Central Time Zone as the Time zone. To get the equivalent date time representation of a particular date time, we have to use the ConvertTime method and pass the TimeZoneInfo object as shown below

DateTime dateTime = TimeZoneInfo.ConvertTime(DateTime.Now, timezone);

So altogether, to get the current date and time for the Central Time zone, the final code looks like below

TimeZoneInfo timezone = TimeZoneInfo.FindSystemTimeZoneById("Central Time zone");

DateTime dateTime = TimeZoneInfo.ConvertTime(DateTime.Now, timezone);

No comments: