I am using an input field to get a date which is stored as "yyyy-mm-dd" and I want to convert it to a more readable format to display with html, so 2000-05-03 will look something like May 3, 2000. Is there an easy way to do this?
CodePudding user response:
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
CodePudding user response:
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
CodePudding user response:
function join(t, a, s) {
function format(m) {
let f = new Intl.DateTimeFormat('en', m);
return f.format(t);
}
return a.map(format).join(s);
}
let a = [{day: 'numeric'}, {month: 'short'}, {year: 'numeric'}];
let s = join(new Date, a, '-');
console.log(s);