EN
TypeScript - day name for specific Date
0
points
In this article, we would like to show you how to get the day name for a specific date in TypeScript.
Quick solution:
const DAY_NAMES: string[] = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
const now: Date = new Date();
const day: string = 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 TypeScript versions.
Practical example
const DAY_NAMES: string[] = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
const getDayName = (date: Date): string => {
return DAY_NAMES[date.getDay()]; // day from 0 (Sunday) to 6 (Saturday)
};
// Usage example:
const now: Date = new Date();
console.log(getDayName(now));
Example output:
Thursday