How to set time zone for ASP.NET MVC 5 application

2019-06-27 00:13发布

问题:

I have quite large web application, which is developed with ASP.NET MVC 5 and MsSql 2008. On my PC, I have +0700 UTC, but on my shared hosting I have other time zone.

This code gives me correct DateTime.

DateTime utcTime = DateTime.UtcNow;

string zoneID = "N. Central Asia Standard Time"; 

TimeZoneInfo myZone = TimeZoneInfo.FindSystemTimeZoneById(zoneID);
DateTime custDateTime = TimeZoneInfo.ConvertTimeFromUtc(utcTime, myZone);

Console.WriteLine(custDateTime.ToString());

Unfortunately, I have many places, where I work with date and time. I afraid, that I forget anywhere to change code.

Is there simple way to set right time zone for my web application?

P.S. All users of my application have same time zone.

回答1:

If Internationalization is important to you, you can use this.

I had very good results by understanding what was written there.

MVC Framework will know how to treat your country, language, currencies, date-times format, timestamps, etc.