Moment.js没有显示正确的时间

时间:2019-04-17 07:16:53

标签: momentjs dst

在我的计算机上本地运行node.js项目时,moment.js显示正确的时间(2019-10-28T07:00:00.000Z,因为对4月DST +02:00和+01:00的DST +02:00进行了调整在十月)):

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 }

但是在我的服务器上,我得到了:

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 }

日期错误地设置为2019-10-28T06:00:00:000Z。 我已使用sudo timedatectl set-timezone Europe/Oslo

在服务器上设置了时区

如果我在服务器上使用moment.isDST()作为日期,它将正确地给我true在四月和false在十月。

我不确定如何在服务器上或在我的代码中解决此问题?

1 个答案:

答案 0 :(得分:1)

我想我解决了。通过回答来自Styx的问题,我终于意识到,在docker容器中运行的服务器应用程序没有正确的时区。通过在environment: TZ: "Europe/Oslo"文件中设置docker-compose.yml,可以解决此问题,现在我可以得到正确的时间。