Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Paul Sterling 718 posts 1534 karma points MVP 8x admin c-trib
    Nov 27, 2018 @ 00:40
    Paul Sterling
    0

    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?

    1. Is it best to store all DateTime values as UTC and then deal with converting to each user's correct "local" time when rendering?
    2. Or is it best to store the values in the correct "local" time - assuming I know that up front?
    3. 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);
    
  • Jonas Söderström 8 posts 78 karma points
    Aug 16, 2019 @ 11:06
    Jonas Söderström
    0

    Interesting question! Which solution have you chosen to use?

  • Paul Sterling 718 posts 1534 karma points MVP 8x admin c-trib
    Aug 16, 2019 @ 14:53
    Paul Sterling
    0

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

Please Sign in or register to post replies

Write your reply to:

Draft