1

How to get days of the week in full?

Here is my code:

var date = new Date();

var n = date.toDateString();

I expect the output of to show Wednesday, May 22 2019 but it shows: Wed May 22 2019

ikos23
  • 3,661
  • 9
  • 30
  • 47
Shawn
  • 13
  • 2
  • Would this link help? https://stackoverflow.com/a/3552496/11330560 You'll need to install this simple package then: https://www.npmjs.com/package/dateformat – Matin Sasan May 22 '19 at 19:35

5 Answers5

1

class Foo {
    getFormattedDate() {  
      const date = new Date();
      const dateFormatOptions = { 
        weekday: 'long', 
        year: 'numeric', 
        month: 'long', 
        day: 'numeric' 
      };
      return date.toLocaleDateString('en-GB', dateFormatOptions);
    }
}

const foo = new Foo();
console.log(foo.getFormattedDate())

This is something you could do. Pro of this solution is that you can easy add different supported locales.

Marco Knol
  • 96
  • 7
1

The default Date object in JavaScript has limited support for formatting in the toLocaleDateString() method: Date​.prototype​.toLocale​Date​String() - JavaScript | MDN

There are some limitations (e.g., not all features are supported in some mobile browsers), but it's worth a look to see if it's got something that you can use, before reinventing the wheel.

For example, the below code will get you pretty close to the format that you are looking for (just has an extra comma between the date and year values):

var today = new Date();
var formatOptions = {weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' };

console.log(today.toLocaleDateString('en-US', formatOptions));
talemyn
  • 7,110
  • 4
  • 25
  • 45
  • Theoretically the right locale for the wanted output was 'en-GB'. Really nice that we posted almost at the same time the same solution. – Marco Knol May 23 '19 at 05:46
  • @LittleGnome - I looked at "en-GB", but the order of the two middle date elements were swapped ("‎Thursday‎, ‎**23 ‎May**‎ ‎2019") from what the OP asked for, so "en-US" seemed like the more appropriate option. – talemyn May 23 '19 at 15:49
0

If you don't like the abbreviations, you have to construct your own string out of the information the Date object provides. Unfortunately it just returns integers from 0-6 for the day of the week and 0-11 for the month - so you have to setup an array with the matching names.

Here's an example:

var now = new Date();
var days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
var months = ["January", "February", "March", "April", "May", "June", "July", "October", "November", "December"];
console.log(days[now.getDay()] + ", " + months[now.getMonth()] + " " + now.getDate() + " " + now.getFullYear());
obscure
  • 8,071
  • 2
  • 9
  • 30
0

In javascript Date object you can only get an integer value representing a specific day by using getDay() method. What you can do is to extend the Date.prototype by creating a custom method on Date.prototype like the following :

Date.prototype.getFullDayName = function(){

    var days = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'];
    var fullDay = this.toDateString().replace(/.+?\s/,days[this.getDay()]+" ")
    return fullDay
}

Above method will give you the full day name from your date object.Then call it like

 date.getFullDayName()
 // "Thursday May 23 2019"
AL-zami
  • 7,637
  • 11
  • 53
  • 104
0

You can use moment.js library in order to achieve this date format.

This code snippet will print to the console the date in the format you requested (in case you downloaded and imported moment.js).

dddd represents the day of the week full name

const start = moment('2016-02-27');
console.log(start.format('dddd YYYY MM DD'));

> Output : "Saturday 2016 02 27"
Omri Grossman
  • 85
  • 1
  • 10