Reputation: 65
We have a web app running in North UK for which we have explicitly specified a time zone using WEBSITE_TIME_ZONE
in App Settings. It's a multi tenant SaaS application with Shared Database architecture. Now we have a new client from US, who is going to use the app. but being in a different timezone it is changing dates and times that are stored in the database.
What should be the best practice to get the app working correctly for different time zones? Should I deploy a separate website for each time zone or should I handle it in code?
Upvotes: 2
Views: 2017
Reputation: 241475
A few things:
The best practice for server-side code, regardless of language or platform, is for it to be time-zone independent. That means not making any hard-coded dependencies on a single time zone, whether that comes from a config file, hosting setting, or server setting.
DateTime.Now
, DateTimeKind.Local
, TimeZoneInfo.Local
, and related APIs, because they take their time zone from the server setting.Instead, use APIs that work with UTC, and/or with specific time zones or time zone offsets.
DateTime.UtcNow
, DateTimeOffset
, TimeZoneInfo.ConvertTime
, TimeZoneInfo.FindSystemTimeZoneById
, and several others. Alternatively, consider using Noda Time, which offers a more comprehensive and consistent API.Generally, an application should track the time zone of a user and/or location as a separate field, stored as a string time zone identifier, either in Windows time format, or IANA time zone format. Read the timezone tag wiki for more details (the section titled "Time Zone Databases").
Regarding the WEBSITE_TIME_ZONE
setting: Only use when all of the following are true:
DateTime.Now
.My personal opinion is that the WEBSITE_TIME_ZONE
setting should not exist. There is no need for it in a properly designed application. It is a crutch that should be using sparingly and as a last resort only.
Upvotes: 7