-1

I am using an input field to get a date which is stored in a ISO format like "yyyy-mm-dd".

I want to convert it to a human-readable format to display in the HTML, so 2000-05-03 will look something like May 3, 2000.

How can I transform it from ISO format to American format?

Skatox
  • 4,237
  • 12
  • 42
  • 47
Dalia E
  • 19
  • 1
  • There are many, many questions with good answers on [parsing](https://stackoverflow.com/search?q=%5Bjavascript%5D+parse+date+string) and [formatting](https://stackoverflow.com/search?q=%5Bjavascript%5D+format+date) dates. – RobG Mar 10 '22 at 11:51

2 Answers2

1

Yes, you can use toLocateDateString() for it. And with the options format the date.

For your example it will be like this:

const date = new Date("2000-05-03");
date.toLocaleString('en-US', { year: 'numeric', month: 'long', day: 'numeric' })
console.log(date);

Will print May 3, 2000

Skatox
  • 4,237
  • 12
  • 42
  • 47
  • 1
    Thank you! I got it to work lie this: date_string = date.toLocaleString('en-US', { year: 'numeric', month: 'long', day: 'numeric' }) – Dalia E Mar 10 '22 at 04:51
  • For anyone with a negative offset, the above returns a date for 2 May, not 3 May. See [*Why does Date.parse give incorrect results?*](https://stackoverflow.com/questions/2587345/why-does-date-parse-give-incorrect-results) – RobG Mar 10 '22 at 11:49
0

You can achieve it using the below code:

var options = { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' };
var today  = new Date();

console.log(today.toLocaleDateString("en-US", options));

Check here for more info - Another Date format related question

Ankit Saxena
  • 1,067
  • 1
  • 4
  • 16