Date: 2022jun28
Language: javaScript
Q. Javascript: format date/time for display
A. Use date.toLocaleDateString()
function prettyTime() {
const date = new Date();
const options = {
day: 'numeric',
month: 'long',
year: 'numeric',
hour: 'numeric',
minute: 'numeric',
second: 'numeric'
};
return date.toLocaleDateString('en-US', options);
}
You can even do it all in one statement:
function prettyTimeOneStatement() {
return new Date().toLocaleDateString('en-US',
{ day: 'numeric', month: 'long', year: 'numeric',
hour: 'numeric', minute: 'numeric', second: 'numeric' });
}