EN
TypeScript - get first day of month
0 points
In this article, we would like to show you how to get the first day of the month in TypeScript.
Quick solution:
xxxxxxxxxx
1
const date: Date = new Date('Mon Aug 02 2021');
2
const firstDay: Date = new Date(date.getFullYear(), date.getMonth(), 1);
3
4
console.log(firstDay); // 2021-07-31T22:00:00.000Z
In this example, we use:
date.getFullYear()
- to get the year of the specified date,date.getMonth()
- to get the month of the specified date,1
in theDate()
constructor stands for the day of the month.
xxxxxxxxxx
1
const date: Date = new Date('Mon Aug 02 2021');
2
const firstDay: Date = new Date(date.getFullYear(), date.getMonth(), 1);
3
4
console.log(firstDay); // 2021-07-31T22:00:00.000Z
Output:
xxxxxxxxxx
1
2021-07-31T22:00:00.000Z