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 line of code given below in C#.

  1. 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 time zone identifier.

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

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

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

  1. using System;  
  2.   
  3. namespace GetAnyTimeZoneInTheWorld  
  4. {  
  5.     class Program  
  6.     {  
  7.         private static TimeZoneInfo India_Standard_Time = TimeZoneInfo.FindSystemTimeZoneById("India Standard Time");  
  8.         private static TimeZoneInfo Eastern_Standard_Time = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");  
  9.         private static TimeZoneInfo Tokyo_Standard_Time = TimeZoneInfo.FindSystemTimeZoneById("Tokyo Standard Time");  
  10.           
  11.         static void Main(string[] args)  
  12.         {  
  13.             DateTime dateTime_Indian = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, India_Standard_Time);  
  14.             DateTime dateTime_Eastern = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, Eastern_Standard_Time);  
  15.             DateTime dateTime_Tokyo = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, Tokyo_Standard_Time);  
  16.             DateTime dateTime = DateTime.Now;  
  17.             Console.WriteLine("India- " + dateTime_Indian);  
  18.             Console.WriteLine("Eastern- " + dateTime_Eastern);  
  19.             Console.WriteLine("Tokyo- " + dateTime_Tokyo);  
  20.             Console.ReadKey();  
  21.         }          
  22.     }  
  23. }   

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.

Ebook Download
View all
Learn
View all