-1

When running my node.js project locally on my computer, moment.js is showing correct time (2019-10-28T07:00:00.000Z because moment have adjusted for DST +02:00 in april and +01:00 in october)):

Moment {
  _isAMomentObject: true,
  _i: '2019-04-15T06:00:00.000Z',
  _f: 'YYYY-MM-DDTHH:mm:ss.SSSSZ',
  _tzm: 0,
  _isUTC: false,
  _pf:
   { empty: false,
     unusedTokens: [],
     unusedInput: [],
     overflow: -1,
     charsLeftOver: 0,
     nullInput: false,
     invalidMonth: null,
     invalidFormat: false,
     userInvalidated: false,
     iso: true,
     parsedDateParts: [ 2019, 3, 15, 6, 0, 0, 0 ],
     meridiem: undefined,
     rfc2822: false,
     weekdayMismatch: false },
  _locale:
   Locale {
     _calendar:
      { sameDay: '[Today at] LT',
        nextDay: '[Tomorrow at] LT',
        nextWeek: 'dddd [at] LT',
        lastDay: '[Yesterday at] LT',
        lastWeek: '[Last] dddd [at] LT',
        sameElse: 'L' },
     _longDateFormat:
      { LTS: 'h:mm:ss A',
        LT: 'h:mm A',
        L: 'MM/DD/YYYY',
        LL: 'MMMM D, YYYY',
        LLL: 'MMMM D, YYYY h:mm A',
        LLLL: 'dddd, MMMM D, YYYY h:mm A' },
     _invalidDate: 'Invalid date',
     ordinal: [Function: ordinal],
     _dayOfMonthOrdinalParse: /\d{1,2}(th|st|nd|rd)/,
     _relativeTime:
      { future: 'in %s',
        past: '%s ago',
        s: 'a few seconds',
        ss: '%d seconds',
        m: 'a minute',
        mm: '%d minutes',
        h: 'an hour',
        hh: '%d hours',
        d: 'a day',
        dd: '%d days',
        M: 'a month',
        MM: '%d months',
        y: 'a year',
        yy: '%d years' },
     _months:
      [ 'January',
        'February',
        'March',
        'April',
        'May',
        'June',
        'July',
        'August',
        'September',
        'October',
        'November',
        'December' ],
     _monthsShort:
      [ 'Jan',
        'Feb',
        'Mar',
        'Apr',
        'May',
        'Jun',
        'Jul',
        'Aug',
        'Sep',
        'Oct',
        'Nov',
        'Dec' ],
     _week: { dow: 0, doy: 6 },
     _weekdays:
      [ 'Sunday',
        'Monday',
        'Tuesday',
        'Wednesday',
        'Thursday',
        'Friday',
        'Saturday' ],
     _weekdaysMin: [ 'Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa' ],
     _weekdaysShort: [ 'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat' ],
     _meridiemParse: /[ap]\.?m?\.?/i,
     _abbr: 'en',
     _config:
      { calendar: [Object],
        longDateFormat: [Object],
        invalidDate: 'Invalid date',
        ordinal: [Function: ordinal],
        dayOfMonthOrdinalParse: /\d{1,2}(th|st|nd|rd)/,
        relativeTime: [Object],
        months: [Array],
        monthsShort: [Array],
        week: [Object],
        weekdays: [Array],
        weekdaysMin: [Array],
        weekdaysShort: [Array],
        meridiemParse: /[ap]\.?m?\.?/i,
        abbr: 'en' },
     _dayOfMonthOrdinalParseLenient: /\d{1,2}(th|st|nd|rd)|\d{1,2}/ },
  _a: [ 2019, 3, 15, 6, 0, 0, 0 ],
  _d: 2019-10-28T07:00:00.000Z,
  _isValid: true,
  _z: null }

But on my server, I get this:

Moment {
  _isAMomentObject: true,
  _i: '2019-04-15T06:00:00.000Z',
  _f: 'YYYY-MM-DDTHH:mm:ss.SSSSZ',
     week: [Object],
     weekdays: [Array],
     weekdaysMin: [Array],
     weekdaysShort: [Array],
     meridiemParse: /[ap]\.?m?\.?/i,
     abbr: 'en' },
  _dayOfMonthOrdinalParseLenient: /\d{1,2}(th|st|nd|rd)|\d{1,2}/ },
  _a: [ 2019, 3, 15, 6, 0, 0, 0 ],
  _d: 2019-10-28T06:00:00.000Z,
  _isValid: true,
  _z: null }

The date is incorrectly set to 2019-10-28T06:00:00:000Z. I have set the timezone on the server with sudo timedatectl set-timezone Europe/Oslo

If I am using moment.isDST() for dates on the server, it correctly gives my true in April and false in October.

I am unsure how to solve this, on the server or in my code?

Ivan C Myrvold
  • 680
  • 7
  • 23
  • What was the original date? Also, you meant "-1 in october", right? – Styx Apr 17 '19 at 07:41
  • The original date was 2019-04-15T06:00:00.000Z entered in April, with time UTC +02:00 and DST. In October we don't have DST in Oslo, so the correct time shown in October will be 2019-10-28T07:00:00.000Z because we have UTC +01:00 – Ivan C Myrvold Apr 17 '19 at 07:48
  • Let's see if I understood your problem correctly. You're creating moment object with `2019-04-15T06:00:00.000Z`, in your local timezone it should be `2019-04-15T08:00:00.000 UTC+02:00` and that's correct. After adding to this date, for example, `6 months`, it should be (in your local timezone): `2019-10-15T07:00:00.000 UTC+01:00`, but it isn't? – Styx Apr 17 '19 at 08:11
  • Yes, that is correct. My local desktop computer running the node.js does this correctly, but not my Linux server – Ivan C Myrvold Apr 17 '19 at 08:14
  • And what exactly is the datetime in your local timezone (in october) then? What is the output of`dt.toLocaleString()`? – Styx Apr 17 '19 at 08:15
  • 2019-10-28T06:00:00.000Z (DST ends 2019-10-27, so I will use the 28th here, not 15th) and in local time this will be 2019-10-28T07:00:000 UTC+01:00 – Ivan C Myrvold Apr 17 '19 at 08:18
  • What _is_ the output of command I mentioned above for october date? – Styx Apr 17 '19 at 08:27
  • My local desktop computer gives me: Mon Oct 28 2019 08:00:00 GMT+0100, and my server gives me: Mon Oct 28 2019 06:00:00 GMT+0000 – Ivan C Myrvold Apr 17 '19 at 08:28
  • Here is your problem. Your server thinks that its local timezone is UTC. – Styx Apr 17 '19 at 08:31
  • But if I run timedatectl on my server, I get: Local time: Wed 2019-04-17 10:31:28 CEST Universal time: Wed 2019-04-17 08:31:28 UTC RTC time: Wed 2019-04-17 08:31:29 Time zone: Europe/Oslo (CEST, +0200) System clock synchronized: yes systemd-timesyncd.service active: yes RTC in local TZ: no – Ivan C Myrvold Apr 17 '19 at 08:32
  • Doesn't moment obtain timezone from the computer it runs on? – Ivan C Myrvold Apr 17 '19 at 08:34
  • What is the output of `moment().utcOffset()`? – Styx Apr 17 '19 at 08:35
  • My local computer gives me: 60, and the server 0 for the dates in October – Ivan C Myrvold Apr 17 '19 at 08:39
  • Hi. Please read about [Internal Properties](http://momentjs.com/guides/#/lib-concepts/internal-properties/) in the Moment.js user guide, and in [this Stack Overflow answer](https://stackoverflow.com/questions/28126529/momentjs-internal-object-what-is-d-vs-i). (Pasting internal properties here does not help us understand your question.) – Matt Johnson-Pint Apr 18 '19 at 22:24

1 Answers1

1

I think I solved it. By answering the from Styx questions, I finally realized that the server app running in a docker container didn't have the correct timezone. By setting environment: TZ: "Europe/Oslo" in the docker-compose.yml file this fixed the problem, and I am getting correct time now.

Ivan C Myrvold
  • 680
  • 7
  • 23
  • Generally speaking, one should not design server-side applications such that they rely on a specific local time zone. Though this may resolve the problem you have at the moment, it doesn't set you up well for success going forward. Consider if your application ever needs to support users from a second time zone, or more. You'll have painted yourself in a corner, so to speak. – Matt Johnson-Pint Apr 18 '19 at 22:28