Reputation: 5174
I am trying to generate a EU date, which is typically in the format of DD/MM/YYYY
.
// August 1st, 2018, EU format is DD/MM/YYYY
const myDate = moment('01/08/2018').locale('fr');
console.log(myDate.format('MM/DD/YYYY'));
Try pasting this code in momentjs.com console, and it doesn't work.
I expected 08/01/2018
, however I get 01/08/2018
back. It seems like it doesn't respect the locale date format.
Anyone have any idea how to fix this?
Upvotes: 1
Views: 1817
Reputation: 23859
On MomentJS site it is clearly specified that:
For consistent results parsing anything other than ISO 8601 strings, you should use String + Format.
You need to pass the format, as the string is not in ISO format.
If you need to leverage locale information, use the script which has support for locale information. Then, you can set the locale using moment.locale
first, and then use L
format specifier (for short date) to parse the date as per the locale's specifications.
See the demo below:
moment.locale('fr');
const myDate = moment('01/08/2018', 'L');
console.log(myDate.format('MM/DD/YYYY'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment-with-locales.min.js"></script>
Upvotes: 6
Reputation: 563
When using moment locales you will either need to import each language file you require into the page. They are normally located in /locale/fr.js.
If you require lots of language files to be loaded you can instead link to moment-with-locales.js which is served alongside moment.js in most CDNs or in the download from Moment's website.
By default Moment ships with 'en' as the language which is actually en-US
Upvotes: 1