开发者

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

开发者 https://www.devze.com 2023-01-06 13:25 出处:网络
My Applicat开发者_StackOverflow中文版ion hosted at server in time zone which differs from mine. All date in database is not correct for my time zone.

My Applicat开发者_StackOverflow中文版ion 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


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...


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消