Reputation: 3937
How should I convert a date format of YYYY-MM-DD
into a human readable format %e %B %Y
in JS by taking into an account a different language from system's locale?
In PHP it would be like
<?php echo trim(strftime('%e %B %Y', strtotime('2019-07-31'))); ?>
// Renders: 31 July 2019
I want to have the same, but with a corresponding language locale, for example "French format" so it will become : 31 Juillet 2019
==== UPDATED ====
As mention by @Baljinder Singh, solution below from the link, works perfectly
console.log(
new Date('2019-07-31').toLocaleDateString('fr-FR', {
year: 'numeric',
month: 'long',
day: 'numeric'
})
)
Upvotes: 0
Views: 1617
Reputation: 2111
If it's browser-specific then you can make it dynamic with window.navigator.language
.
const date = new Date('2019-07-31').toLocaleDateString(window.navigator.language, {
year: 'numeric',
month: 'long',
day: 'numeric',
});
console.log(date);
Notice: Working fine in chrome and firefox.
Upvotes: 2
Reputation: 92367
Try
let d = new Date('2019-08-20');
let s = d.toLocaleDateString(navigator.languages,{day:"numeric", month:"long", year: "numeric"});
console.log(s);
Upvotes: 1