4

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.

Jed
  • 10,649
  • 19
  • 81
  • 125
  • This should point you in the right direction: http://stackoverflow.com/questions/10032456/how-to-get-next-seven-days-from-x-and-format-in-js/10032685#10032685 – James Hill Nov 22 '16 at 22:25
  • I know how to implement it myself, but what I was really trying to accomplish is a hacky/lazy way of getting the full names of the month/day in the language of the client – Jed Nov 22 '16 at 22:30
  • Possible duplicate of [Where can I find documentation on formatting a date in JavaScript?](http://stackoverflow.com/questions/1056728/where-can-i-find-documentation-on-formatting-a-date-in-javascript) – Heretic Monkey Nov 22 '16 at 22:43

2 Answers2

3

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

Example:

console.log(
  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 is way more comfortable.

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

mplungjan
  • 169,008
  • 28
  • 173
  • 236
Josa
  • 716
  • 6
  • 10
0

As Josa's answer implies, it is possible to get the full names for the weekday and month (for most modern browsers).

I used the .toLocaleDateString() to extract the full names of the weekday and month in the language of the client successfully using Chrome and IE11 (it does NOT work in Safari 9).

Extending the idea of getting the full names from the browser, I was curious if we could, then, get the name of the weekday and the name of the month in the language that matches the client's locale. The following experiment shows that that it is possible (although, not reliable).

For Experiment/Fun:

The following function will return an object that includes the name of the Weekday and Month for a particular date in the language of a particular locale-code.

Fiddle

function getWeekdayAndMonthNamesByLocaleCode(localeCode, date) {
  var Names = {
    Weekday: "",
    Month: ""
  };

  if (!(date instanceof Date && !isNaN(date.valueOf()))) {
    date = new Date();
  }

  if (localeCode == null || localeCode.length < 2) {
    localeCode = navigator.language;
  }

  Names.Month = date.toLocaleDateString(localeCode, {
    month: 'long',
  });

  Names.Weekday = date.toLocaleDateString(localeCode, {
    weekday: 'long'
  });

  return Names;
}

I haven't tested the function for browser compatibility and I certainly wouldn't rely on it to work for all languages, but I thought it was interesting enough to post here for others to experiment with.

Jed
  • 10,649
  • 19
  • 81
  • 125