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
: 1896 At:- 4/25/2018 11:09:19 AM
C# datetime timezone conversion

1 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);

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.

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

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

Full Name *

Email *

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

Subscribe Now

Subscribe to our weekly Newsletter & Keep getting latest article/questions in your inbox weekly