javascriptdatetimemeanjs

How to get day name in a date in node js


I have a date like this

2017-06-23 and my desired output is

Friday June 23 2017

 v.npi.appointment_dates[j] = date.toLocaleString('en-US', {year: 'numeric', month: 'long', day: 'numeric' });

But because of the day: 'numeric' I am missing the day name. Can anyone please help me?


Solution

  • You need to include weekday: 'long' in the options object:

    var date = new Date();
    console.log(date.toLocaleString('en-US', {
          weekday: 'long',
          year: 'numeric',
          month: 'long',
          day: 'numeric'
        }));

    If you want to adopt the the browser default language, use undefined:

    var date = new Date();
    console.log(date.toLocaleString(undefined, {
          weekday: 'long',
          year: 'numeric',
          month: 'long',
          day: 'numeric'
        }));

    Even with options support, the result of toLocaleString is still implementation dependent as the "locale" option is actually a language code, which may not be what the user expects even if they speak that language and the browser developers may not have chosen the expected format.