Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

255
Views
How to change the locale in moment.js

I know this probably looks like a duplicate question but I am unable to change my locale using moment in my React/ES6 setup.

This is what I have been able to gather from the internet but it doesn't work

import moment from 'moment';
import 'moment/min/locales';

moment.locale('en-gb');
moment('31/08/2021').format('YYYY-MM-DD') \\ returns 'Invalid Date'

Where am I going wrong?

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

This may not be the perfect way to do it but it definitely works.

    locale = 'en-GB';
    value = '07/06/2021';


    moment.locale(locale);
    const localeData = moment.localeData();
    const format = localeData.longDateFormat('L');
    console.log(moment(value, format).format('YYYY-MM-DD')); // '2021-06-07'
about 4 years ago · Juan Pablo Isaza Report

0

It does not look like a moment locale issue. Try specifying the right input format to parse a moment string.

More info can be found at https://momentjs.com/docs/#/parsing/string/

console.log(moment("31/08/2021", "DD/MM/YYYY").format("YYYY-MM-DD"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>

about 4 years ago · Juan Pablo Isaza Report

0

Try this:

moment('31/08/2021', 'DD-MM-YYYY').format('YYYY-MM-DD')
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!