Skip to main content

Get Any Date And Time As Per Time Zone In C#

We can use any date time in the world, as per our requirement. Here, I am explaining through a sample console application.

We can get the current date time in our current Time Zone, using lines of code given below in C#.

DateTime dateTime = DateTime.Now;

 

If we want to use any other country’s date time, then we have to use the FindSystemTimeZoneById method to retrieve the specific date time by passing the timezone identifier.

For example, if we are going to use Eastern Standard Time, then we can use the code given below.

DateTime dateTime_Eastern = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, Eastern_Standard_Time);

 

The sample code is given below for the console Application as a reference.

using System;  
  
namespace GetAnyTimeZoneInTheWorld  
{  
    class Program  
    {  
        private static TimeZoneInfo India_Standard_Time = TimeZoneInfo.FindSystemTimeZoneById("India Standard Time");  
        private static TimeZoneInfo Eastern_Standard_Time = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");  
        private static TimeZoneInfo Tokyo_Standard_Time = TimeZoneInfo.FindSystemTimeZoneById("Tokyo Standard Time");  
          
        static void Main(string[] args)  
        {  
            DateTime dateTime_Indian = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, India_Standard_Time);  
            DateTime dateTime_Eastern = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, Eastern_Standard_Time);  
            DateTime dateTime_Tokyo = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, Tokyo_Standard_Time);  
            DateTime dateTime = DateTime.Now;  
            Console.WriteLine("India- " + dateTime_Indian);  
            Console.WriteLine("Eastern- " + dateTime_Eastern);  
            Console.WriteLine("Tokyo- " + dateTime_Tokyo);  
            Console.ReadKey();  
        }          
    }  
}

 

The screenshot is given below of the output of the console application.

In this easy way, we can get the date time of any time zone, using C#

Download the zip file from the attachment for the source code of the sample application.

Leave a Reply