Thursday, May 23, 2013

Asp.Net How to Convert To Different Time Zones Using C#


Here I will Explain How To Get The Server Times Of Different Countries Using C#

Some Times We Come Across Different Date And Time Zones To Display, So By Using The Following Code You Can Get Different Countries Date And Time.
     
        DateTime now = DateTime.Now;            //Displays Our System Time
        c.Text = "Time is " + now.ToString() + " in your System";      

        string zoneId = "Central European Standard Time";
        TimeZoneInfo tzi = TimeZoneInfo.FindSystemTimeZoneById(zoneId);
        DateTime result = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, tzi);
        a.Text = "Time is " + result + " in Denmark";

        string zoneId1 = "India Standard Time";
        TimeZoneInfo tzi1 = TimeZoneInfo.FindSystemTimeZoneById(zoneId1);
        DateTime result1 = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, tzi1);
        b.Text = "Time is " + result1 + " in India";

        string zoneId2 = "Central Standard Time";
        TimeZoneInfo tzi2 = TimeZoneInfo.FindSystemTimeZoneById(zoneId2);
        DateTime result2 = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, tzi2);
        c.Text = "Time is " + result2 + " in Chicago";

Output:



No comments:

Post a Comment