How to get first and last day of current month in Angular?

Dung Do Tien Oct 27 2020 75

How to get the first and last day of the current month in Angular 8?

I have a simple web application using Angular. I need to get the first and last date of the current month to set the value for a combobox datetime picker. I try as below but I can't get the last date of the current month.

currentDate = new Date();
ngOnInit() {
    const firstDay = 1;
    const lastDay = currentDate.getDate(); // only return current date

The lastDay variable always returns to the current day, it's not the last day of the month.

Please help me if you have any solutions.

Have 1 answer(s) found.
  • It depends on the constructor of the Date. To get the last day of the current month you can do as below:

    currentDate = new Date();
    lastDate = new Date(this.currentDate.getFullYear(),this.currentDate.getMonth() + 1, 0,23,59,59,999);
    ngOnInit() {
        const firstDay = 1;
        const lastDay = this.lastDate.getDate(); 

    Notice that :

    getFullYear(): return from 1000 - 9999.

    - getMonth(): Return from 0 - 11.

    - getDate():  Returns the day of the month from 1 to 31.

    Marry Christ Oct 27 2020
Leave An Answer
* NOTE: You need Login before leave an answer

* Type maximum 2000 characters.

* All comments have to wait approved before display.

* Please polite comment and respect questions and answers of others.

Popular Tips