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:
xxxxxxxxxx
1
const DAY_NAMES: string[] = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
2
3
const now: Date = new Date();
4
const day: string = 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 TypeScript versions.
xxxxxxxxxx
1
const DAY_NAMES: string[] = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
2
3
const getDayName = (date: Date): string => {
4
return DAY_NAMES[date.getDay()]; // day from 0 (Sunday) to 6 (Saturday)
5
};
6
7
8
// Usage example:
9
10
const now: Date = new Date();
11
12
console.log(getDayName(now));
Example output:
xxxxxxxxxx
1
Thursday