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.
above is the working dotnet fiddle.
Subscribe to our weekly Newsletter & Keep getting latest article/questions in your inbox weekly