how to convert datetime from one timezone to another in C#?


I am working on a C# project in which user can change datetime column time zone using its profile page. For example, when I log in into the system, I can change default time zone (PST) to Indian standard time(IST) after doing all the column of date time should show time-related to IST time zone.

Basically, we are saving all the datetime value in the database as PST time zone, now based on user-selected time zone I need to show datetime.

So I would like to convert my PST datetime (8/16/2016 12:56 PM) to IST or any other timezone selected by user dynamically using C#.

How can I convert datetime from PST to any time zone using C#?


Asked by:- jaya
2
: 7677 At:- 4/25/2018 11:09:19 AM
C# datetime timezone conversion







3 Answers
profileImage Answered by:- Vinnu

Yes, You can convert datetime from one timezone to another in C#, there is an inbuilt function for it, but you should have the current date time timezone.

As you have mentioned you have that and it's PST timezone DateTime and you need the output of the user selected TimeZOne

Suppose the user has selected IST timezone, then your C# code will be

DateTime PSTtimeZoneDatetime= Convert.ToDateTime("8/16/2016 12:56 PM");//your PST timezone datetime

    TimeZoneInfo timeZoneInfo;

   //Set the time zone information to India Standard Time
    timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById("India Standard Time");

    //get PST timeZone as Default for datetime
    TimeZoneInfo PSTTimeZone = TimeZoneInfo.FindSystemTimeZoneById("Pacific SA Standard Time");

  //IST timezone Datetime
   var ISTdateTime =TimeZoneInfo.ConvertTime(PSTtimeZoneDatetime, PSTTimeZone, timeZoneInfo);
		Console.WriteLine(ISTdateTime);

that's it ISTDatetime value is the one you require, simply you can pass the new timezone instead of IST and use the above code.

https://dotnetfiddle.net/St7D9a

above is the working dotnet fiddle.

3
At:- 4/25/2018 2:53:05 PM
thanks for the Fiddle example, which made it the above code clear. 0
By : jaya - at :- 4/30/2018 11:23:23 AM


profileImage Answered by:- neena

If you don't know TimeZone Ids, you can get list of it first

            foreach (TimeZoneInfo z in TimeZoneInfo.GetSystemTimeZones())
            {
                Console.WriteLine(z.Id);
            }

then convert it from onetime to another, by converting input time into UTC first

DateTime inputDateTime= Convert.ToDateTime("8/16/2016 12:56 PM");
DateTime inputTimeInUTC = inputDateTime.ToUniversalTime();

Then get TimeZone Id from above foreach loop as you want, for example I want to convert to "Central America Standard Time"

So, C# code for it will be

TimeZoneInfo timeInfo = TimeZoneInfo.FindSystemTimeZoneById("Central America Standard Time");

and then convert

var outputDateTime = System.TimeZoneInfo.ConvertTimeFromUtc(inputTimeInUTC, timeInfo);

here is complete code

using System;
					
public class Program
{
	public static void Main()
	{
		DateTime inputDateTime= Convert.ToDateTime("8/16/2016 12:56 PM");
		DateTime inputTimeInUTC = inputDateTime.ToUniversalTime();
		TimeZoneInfo timeInfo = TimeZoneInfo.FindSystemTimeZoneById("Central America Standard Time");
		var outputDateTime = System.TimeZoneInfo.ConvertTimeFromUtc(inputTimeInUTC, timeInfo);
		
		Console.WriteLine(outputDateTime);
	}
}

Fiddle: https://dotnetfiddle.net/6AkSy8

Hope it helps.

Also read:

Convert string of "dd/MM/yyyy" format into Datetime using C#?

1
At:- 6/25/2021 3:42:23 PM Updated at:- 6/25/2021 3:48:02 PM


profileImage Answered by:- jon

Change from one timezone to specific timezone 

var date = System.TimeZoneInfo.ConvertTimeFromUtc(
    DateTime.UtcNow, //your time in UTC
    TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time")); // time zone in which we want to convert

This should work.

0
At:- 6/23/2022 6:46:51 AM






Login/Register to answer
Or
Register directly by posting answer/details

Full Name *

Email *




By posting your answer you agree on privacy policy & terms of use