EN
JavaScript - day name for specific Date
3 points
In this article, we would like to show you how to get the day name for a specific date in JavaScript.
Quick solution:
xxxxxxxxxx
1
const DAY_NAMES = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
2
3
const now = new Date();
4
const day = DAY_NAMES[now.getDay()];
5
6
console.log(day);
Note: in the above example we need to provide day names by ourselves to keep legacy with older JavaScript versions.
xxxxxxxxxx
1
const DAY_NAMES = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
2
3
const getDayName = (date) => {
4
return DAY_NAMES[date.getDay()]; // day from 0 (Sunday) to 6 (Saturday)
5
};
6
7
8
// Usage example:
9
10
const now = new Date();
11
12
console.log(getDayName(now));
Example output:
xxxxxxxxxx
1
Thursday