0

I had previously implemented dates in my app based off a timezone stored in the user's profile. As far as I can recall, this worked correctly when originally implemented.

All the dates stored in the database that I'm displaying are in UTC, meaning when I save dates (in C#), I use DateTime.UtcNow.

Here is the momentjs code I'm using in Angular to render my dates with actual values:

moment.utc('2016-05-16T19:14:06.51').utcOffset('-05:00:00').format('M/D/YYYY h:mma');

The -05:00:00 offset is for Eastern Time. I'm expecting the specified UTC date above to result in the display of 3:15pm, but instead is displaying 2:15pm.

To double check I wasn't using a bad date value, I went into MS SQL Management Studio and ran SELECT GETUTCDATE() which returned 2016-05-16 19:44:05.850. It is currently 3:44pm, but it is rendering 2:44pm with the above momentjs code.

What is going on? Am I missing some momentjs code to handle daylight savings or something?

mellis481
  • 4,332
  • 12
  • 71
  • 118

1 Answers1

0

A few things:

  • -05:00:00 is not a time zone, it's an offset. Read "Time Zone != Offset" in the timezone tag wiki.
  • Using utcOffset sets the same fixed offset for all time, so the values you recieved are correct for that offset.
  • There's no way it can know you meant US Eastern Time when you only provide an offset, as many other time zones use that same offset at different points in time.
  • Using the moment-timezone add-on you can specify Eastern time using the America/New_York identifier from the tzdb. Your updated code would be as follows:

    moment.utc('2016-05-16T19:14:06.51').tz('America/New_York').format('M/D/YYYY h:mma');
    
Community
  • 1
  • 1
Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575
  • I originally started w/ the `tz()` time zone code you provided, but I wanted to allow the user to select a time zone in their profile from a finite list, specifically the same 26 values used in Windows time zones. Originally, I tried to find a named time zone for each Windows value, but that proved challenging. Then I saw you could use offset so I just assigned an offset to the Windows values and used `utcOffset()` instead of `tz()`. Is there a way to handle time zones with momentjs using a finite list like I'm trying to because I'm still unclear how to do it? – mellis481 May 17 '16 at 13:16
  • 1
    There's no way to just use an offset. The time zone you are talking about has multiple offsets: -5 during standard time and -4 during daylight time. In addition, there are substantially more than 26 time zones, even in Windows. Read the tag wiki Matt directed you, it answers the questions you are asking. – Maggie Pint May 17 '16 at 15:14
  • Time zone selection is indeed challenging, but that's not what your question was about. You may want to watch [this YouTube video](https://youtu.be/2BdFg5JT9lg), which covers some of the challenges. There's no one right answer for that, except to recognize that an offset alone is not enough. You may also find [my answer to another question](http://stackoverflow.com/a/18407231/634824) useful. – Matt Johnson-Pint May 17 '16 at 16:12
  • Good god. I think now it's time to try to discuss with the product owner whether they'll accept using local time. Thanks for the information! – mellis481 May 17 '16 at 19:43