timezonemomentjsrecurring-events

Using Moment Timezone for Future Events, What should I save?


I have done a fair amount of research, and new to the idea of programming with time.

I have an application where you must be able to schedule a meeting in local time. So you might say "23rd December 2017 at 9am in San Francisco". The Event Location is the perspective of time. It may also recur which is a different issue but related.

When I am creating an event, I know that I should not store future events in UTC. I believe I need:

  1. The Local Date Time
  2. The UTC Offset
  3. The Olson Database ID Europe/London perhaps

However, I plan to use Moment Timezone both locally and on the server portion.

By using this library. How does that change my outlook? Does it mean I no longer have to be concerned about Daylight Saving Time? That if you tell me "2017/12/12 09:00", I could turn that into local time accurately for any timezone in the world?

So all I really need is:

  1. The Local Time and Date (As an ISOString?)
  2. The Olson TimeZone ID.

Later on the server, when I want to send out a Push Notification 1 hour before the Event. These 2 properties (and Moment Timezone) should have me covered?


Solution

  • You can convert local times reliably with moment-timezone very easily:

    var original = moment.tz('2017-12-23T09:00:00', 'America/Los_Angeles');
    
    var easternUS = original.clone().tz('America/New_York');
    var centralEU = original.clone().tz('Europe/Berlin');
    var westAUS = original.clone().tz('Australia/Perth');
    var japan = original.clone().tz('Asia/Tokyo');
    // etc.
    

    (Use .format(...) to produce a string with the desired output.)

    You can also easily calculate the time one hour earlier with any normal Moment functions.

    var earlierUTC = original.clone().utc().subtract(1, 'hour');
    var earlierEastern = easternUS.clone().subtract(1, 'hour');
    

    Yes, as you can see, all you need to store is 2017-12-23T09:00:00 and America/Los_Angeles.

    One might consider storing the event time with an offset, such as 2017-12-23T09:00:00-08:00, but you run into a few problems there:

    There is one point you should think about. What do you do if an event local time is invalid or ambiguous?

    Moment-timezone automatically applies the above suggestions during its conversions, so unless you have a reason to deviate - you don't have to worry about it.