3

I'm parsing a date like this. The locale of moment is set to Germany:

moment("2030-12-31T23:59:59.000Z")

However when I print it it will select the next day:

moment("2030-12-31T23:59:59.000Z").format('L')
-> "01.01.2031"

This is what the moment object looks like after the parse and where we can clearly see that the parsed date is correct:

Moment {_isAMomentObject: true, _i: "2030-12-31T23:59:59.000Z", _f: "YYYY-MM-DDTHH:mm:ss.SSSSZ", _tzm: 0, _isUTC: false, …}
_d
:
Wed Jan 01 2031 00:00:00 GMT+0100 (CET) {}
_f
:
"YYYY-MM-DDTHH:mm:ss.SSSSZ"
_i
:
"2030-12-31T23:59:59.000Z"
_isAMomentObject
:
true
_isUTC
:
false
_isValid
:
true
_locale
:
Locale {_calendar: {…}, _longDateFormat: {…}, _invalidDate: "Invalid date", _ordinal: "%d.", _ordinalParse: /\d{1,2}\./, …}
_pf
:
charsLeftOver
:
0
empty
:
false
invalidFormat
:
false
invalidMonth
:
null
iso
:
true
meridiem
:
undefined
nullInput
:
false
overflow
:
-1
parsedDateParts
:
(7) [2030, 11, 31, 23, 59, 59, 0]
unusedInput
:
[]
unusedTokens
:
[]
userInvalidated
:
false
__proto__
:
Object
_tzm
:
0
__proto__
:
Object

Funnily I'm parsing 2-3 more dates exactly the same way (most January 1. of some year) and they work fine!

It's not a UTC thing I believe.

What is happening here?

Matthias Max
  • 595
  • 1
  • 7
  • 20

1 Answers1

2

Your input ends with Z, so it is considered as UTC.

Use moment.utc instead:

By default, moment parses and displays in local time.

If you want to parse or display a moment in UTC, you can use moment.utc() instead of moment()

moment.locale('de');
var m1 = moment.utc("2030-12-31T23:59:59.000Z").format('L');
console.log(m1);
var m2 = moment("2030-12-31T23:59:59.000Z").format('L');
console.log(m2);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.20.1/moment-with-locales.min.js"></script>
VincenzoC
  • 30,117
  • 12
  • 90
  • 112