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#?
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.
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#?
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.
Subscribe to our weekly Newsletter & Keep getting latest article/questions in your inbox weekly