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
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
The default Date
object in JavaScript has limited support for formatting in the toLocaleDateString()
method: Date.prototype.toLocaleDateString() - 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));
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.
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());
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"
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"