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:
// ONLINE-RUNNER:browser;
const DAY_NAMES = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
const now = new Date();
const day = DAY_NAMES[now.getDay()];
console.log(day);
Note: in the above example we need to provide day names by ourselves to keep legacy with older JavaScript versions.
Practical example
// ONLINE-RUNNER:browser;
const DAY_NAMES = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
const getDayName = (date) => {
return DAY_NAMES[date.getDay()]; // day from 0 (Sunday) to 6 (Saturday)
};
// Usage example:
const now = new Date();
console.log(getDayName(now));
Example output:
Thursday