Search code examples
javascriptreactjsmomentjsdayjs

Momentjs is not converting certain Spanish months to UTC


I am working on a React Native application which is available on two languages (English & Spanish). Getting date in English is fine but for Spanish, Momentjs is not able to convert it to moment object for certain months(in spanish) like Abril, It throws Invalid date as output, while other month like Septiembre is working fine and it is getting converted to moment object.

const aprilDateES = moment("09 Abril, 2021") // Invalid date
const septeDateEs = moment("09 Septiembre, 2021") // Thu Sep 09 2021 00:00:00 GMT+0530

It is also throwing warning for Abril month - Warning message

My goal is to convert all Spanish date/month to moment object so that it can be converted to other format like DD/MM/YYYY and can also be validated using momentJS.

Here's the link to playground - https://replit.com/@DevAk1/MomentJSSpanishDate#src/App.jsx


Solution

  • Change import to -

    import moment from 'moment/min/moment-with-locales'
    

    Then use this -

    const date = moment('09 Abril, 2021', 'DD MMMM, YYYY', 'es').locale('en') 
    // Fri Apr 09 2021 00:00:00 GMT+0530
    

    Note - This is specific to format and language. Please change the source/target language and the date format according to the requirement.