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:
const date: Date = new Date('Mon Aug 02 2021');
const firstDay: Date = new Date(date.getFullYear(), date.getMonth(), 1);
console.log(firstDay); // 2021-07-31T22:00:00.000Z
Practical example
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,1in theDate()constructor stands for the day of the month.
const date: Date = new Date('Mon Aug 02 2021');
const firstDay: Date = new Date(date.getFullYear(), date.getMonth(), 1);
console.log(firstDay); // 2021-07-31T22:00:00.000Z
Output:
2021-07-31T22:00:00.000Z