Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript date of last day of previous month

Tags:

javascript

Let's say I have 3 variables like this:

var Month = 8; // in reality, it's a parameter provided by some user input
var Year = 2011; // same here
var FirstDay = new Date(Year, Month, 1);

Now I want to have the value of the day before the first day of the month in a variable. I'm doing this:

var LastDayPrevMonth = (FirstDay.getDate() - 1);

It's not working as planned. What the right of doing it?

Thanks.

like image 871
frenchie Avatar asked Sep 19 '11 03:09

frenchie


2 Answers

var LastDayPrevMonth = new Date(Year, Month, 0).getDate();
like image 195
Will Avatar answered Oct 22 '22 02:10

Will


var LastDayPrevMonth = new Date(FirstDay);
LastDayPrevMonth.setHours(FirstDay.getHours()-24);
like image 1
zhongshu Avatar answered Oct 22 '22 02:10

zhongshu