Jed Jed - 10 days ago 5
Javascript Question

JavaScript full names for day-of-week and month (or How to get the date names of client locale)

The string that is returned from

(new Date()).toString()
looks something like this:

"Tue Nov 22 2016 14:14:51 GMT-0800 (Pacific Standard Time)"


Is there a built-in method/constructor that we can use that will not abbreviate the day-of-week and/or month? In other words, does JS support this:

"Tuesday November 22 2016 14:14:51 GMT-0800 (Pacific Standard Time)"


The reason for my question is because I'm looking for a lazy/hacky way that we might be able to get the Weekday and Month names in the language of the client.

Answer

If don't need to support old browsers (older than IE 11) you can use toLocalDateString().

Example:

new Date().toLocaleDateString('en-US', {
    weekday: 'long',
    month: 'long',
    day: 'numeric',
    year: 'numeric',
    hour: '2-digit',
    minute: '2-digit',
    second: '2-digit',
    timeZoneName: 'short'
})

But moment.js way more comfortable.

See MDN Date.prototype.toLocaleDateString() for more information.

Comments