Retrieving all time zones and their respective current times


The code will help you to retrieve all the time zones present in the registry and their respective current times. Class used: TimeZoneInfo: It represents any time zone in the world. The TimeZoneInfo class offers significant enhancements over the TimeZone class, which provides only limited functionality.

Instance of TimeZoneInfo class is immutable, i.e., when we instantiate it the value can't be modified.

more about the class can be read here:

http://msdn.microsoft.com/en-us/library/system.timezoneinfo(v=vs.110).aspx




//retrieve all the timezones from regirtsy

var timeZones = TimeZoneInfo.GetSystemTimeZones();
var myTimezne = timeZones.Select(timeZone => timeZone.Id).ToList();

//iterate through all the timezones and print their ids and current date and time as per that id

foreach (var zoneId in myTimezne)
{
//Get current date and time information from the specified TimeZone
var localtime = DateTime.Now;
var timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById(zoneId);
var dataTimeByZoneId = TimeZoneInfo.ConvertTime(localtime, TimeZoneInfo.Local, timeZoneInfo);
Console.WriteLine("Zone : " + zoneId + " DateTime : " + dataTimeByZoneId);
}

Console.ReadLine();


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: