aspirinemaga
aspirinemaga

Reputation: 3937

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

Upvotes: 0

Views: 1617

Answers (2)

Neel Rathod
Neel Rathod

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

Kamil Kiełczewski
Kamil Kiełczewski

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

Related Questions