Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get last day of the month

How can I obtain the last day of the month with the timestamp being 11:59:59 PM?

like image 841
Rod Avatar asked Dec 17 '09 21:12

Rod


People also ask

How do I get the last day of the month in Excel?

=EOMONTH(A2, -1) - returns the last day of the month, one month before the date in cell A2.

How do I get last week of the month?

To get the last weekday in a month (i.e. the last Saturday, the last Friday, the last Monday, etc) you can use a formula based on the EOMONTH and WEEKDAY functions. Inside WEEKDAY, EOMONTH is again used to get the first day of the next month.

How do you get the first day of the month?

Here, we use the EOMONTH function to go to the last day of the previous month. Then, we add 1 to get the first day of the current month. To perform the previous example with the EOMONTH function, we need to use the formula =EOMONTH(A2,-1)+1 in cell B2.


2 Answers

function LastDayOfMonth(Year, Month) {
  return new Date((new Date(Year, Month, 1)) - 1);
}

console.log(LastDayOfMonth(2009, 11))

Example:

> LastDayOfMonth(2009, 11)
Mon Nov 30 2009 23:59:59 GMT+0100 (CET)
like image 103
miku Avatar answered Oct 02 '22 15:10

miku


This will give you last day of current month.

var t= new Date();
alert(new Date(t.getFullYear(), t.getMonth() + 1, 0, 23, 59, 59));
like image 33
Chetan S Avatar answered Oct 02 '22 16:10

Chetan S