Getting a specific time zone DateTime when using Umbraco Cloud
This is more of a request for input than anything else, but I welcome critique too since I may be missing something obvious.
Since Umbraco Cloud infrastructure uses UTC time (like all Azure-based services) but our users are in a variety of (non UTC) time zones- what is the best way to display local DateTime values to the users?
Is it best to store all DateTime values as UTC and then deal with converting to each user's correct "local" time when rendering?
Or is it best to store the values in the correct "local" time - assuming I know that up front?
If I store the UTC value, and my websites also run on Cloud, how do I determine what "local" time is for a specific user? (assuming a user will move between time zones at various times)
In the interest of full-disclosure I'm currently making an assumption that all my users will be in a specific time zone (that's about 65% correct) and storing the time zone specific value.
// we're working in US Central Time
DateTime utcDateTime = DateTime.UtcNow;
TimeZoneInfo ctTimeZone = TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time");
DateTime ctDateTime = TimeZoneInfo.ConvertTimeFromUtc(utcDateTime, ctTimeZone);
I ended up storing DateTime data in the UTC format and then adding a property to my Users for their "home" TimeZone - converting from the stored UTC value to the user's TimeZone value at render time:
// we're working in US Central Time
DateTime utcDateTime = DateTime.UtcNow; \\ or use stored user value
TimeZoneInfo ctTimeZone = TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time");
DateTime ctDateTime = TimeZoneInfo.ConvertTimeFromUtc(utcDateTime, ctTimeZone);
Depending on the scenario I either converted when populating my View Model or in my View, with Razor.
Since I am dealing with only 5 known time zones, it is not too complex.
Another reason I want with storing in UTC and converting on render was the integration we have with several 3rd-party APIs that use "Unix" time. I found that we did less converting this way when passing a value back and forth between "Windows" and "Unix" formats. Something simple like this:
DateTimeOffset dto = new DateTimeOffset(DateTime.UtcNow);
var apiUnixTime = dto.ToUnixTimeSeconds();
Would certainly like to know if there is a better or more conventional approach here.
Getting a specific time zone DateTime when using Umbraco Cloud
This is more of a request for input than anything else, but I welcome critique too since I may be missing something obvious.
Since Umbraco Cloud infrastructure uses UTC time (like all Azure-based services) but our users are in a variety of (non UTC) time zones- what is the best way to display local DateTime values to the users?
In the interest of full-disclosure I'm currently making an assumption that all my users will be in a specific time zone (that's about 65% correct) and storing the time zone specific value.
Interesting question! Which solution have you chosen to use?
Hi Jonas -
I ended up storing
DateTime
data in the UTC format and then adding a property to my Users for their "home"TimeZone
- converting from the stored UTC value to the user'sTimeZone
value at render time:Depending on the scenario I either converted when populating my View Model or in my View, with Razor.
Since I am dealing with only 5 known time zones, it is not too complex.
Another reason I want with storing in UTC and converting on render was the integration we have with several 3rd-party APIs that use "Unix" time. I found that we did less converting this way when passing a value back and forth between "Windows" and "Unix" formats. Something simple like this:
Would certainly like to know if there is a better or more conventional approach here.
is working on a reply...