简体   繁体   中英

Convert JS date format into localized human readable format

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' }) ) 

Try

 let d = new Date('2019-08-20'); let s = d.toLocaleDateString(navigator.languages,{day:"numeric", month:"long", year: "numeric"}); console.log(s); 

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM