ck3g
ck3g

Reputation: 5929

How can I set TimeZone for my ASP.NET (MVC) Application

My Application hosted at server in time zone which differs from mine. All date in database is not correct for my time zone. How can I set my time zone for Application or how I can convert date to my time zone on output

Upvotes: 3

Views: 4015

Answers (2)

Adnan Sarwar
Adnan Sarwar

Reputation: 139

There's very easy way to do it. Simply get the current UTC time and your timezone. Convert UTC to your timezone. Here's how you do it.

 DateTime date1 = DateTime.UtcNow;                   
 TimeZoneInfo tz = TimeZoneInfo.FindSystemTimeZoneById("YOUR TIME ZONE (e.g. Pakistan Standard Time)");               
 DateTime date2 = TimeZoneInfo.ConvertTime(date1, tz); 

Set your Time Zone in tz and then use "date2" anywhere.

Upvotes: 1

Jon Skeet
Jon Skeet

Reputation: 1503120

Don't set the time zone for the process - use TimeZoneInfo from .NET 3.5 and higher to perform the relevant conversions.

Of course, that assumes you know the time zone that the data will come back in from the database... usually database records are kept in UTC, but not always...

Upvotes: 3

Related Questions