Moment-timezone failing to parse date


I’m having some issues with moment-timezone that baffle me. The same issue exists if I use just Moment. This is a react component in a next.js project.

This is my code:

useEffect(() => {
    const test = moment.utc('December 01, 2021 01:00pm', 'MMMM DD, YYYY hh:mma')
    console.log(test)
}, [])

And the output is:

    Moment {_isAMomentObject: true, _i: 'December 01, 2021 01:00pm', _f: 'MMMM DD, YYYY hh:mma', _strict: true, _isUTC: true, …}
_a: (7) [2021, 0, 1, 1, 0, 0, 0]
_d: Invalid Date {}
_f: "MMMM DD, YYYY hh:mma"
_i: "December 01, 2021 01:00pm"
_isAMomentObject: true
_isUTC: true
_isValid: false
_locale: Locale {_calendar: {…}, _longDateFormat: {…}, _invalidDate: 'Invalid date', _dayOfMonthOrdinalParse: /d{1,2}日/, ordinal: ƒ, …}
_pf: {empty: false, unusedTokens: Array(1), unusedInput: Array(1), overflow: -1, charsLeftOver: 2, …}
_strict: true
_z: null

I can’t figure out a problem with the date. Anything I put in says invalid. If I implement the same thing by appending a cdn-hosted moment script inside onEffect, and accessing it using window.moment after a window load event, it works.

Does anyone see the problem?

Source: React – Stack Overflow

November 12, 2021
Category : News
Tags: moment-timezone | momentjs | next.js | reactjs

Leave a Reply

Your email address will not be published. Required fields are marked *

Sitemap | Terms | Privacy | Cookies | Advertising

Senior Software Developer

Creator of @LzoMedia I am a backend software developer based in London who likes beautiful code and has an adherence to standards & love's open-source.