How can I obtain the last day of the month with the timestamp being 11:59:59 PM?
Asked
Active
Viewed 6.2k times
11 Answers
41
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)

S M Sajjad Salam
- 21
- 8

miku
- 181,842
- 47
- 306
- 310
-
1+1 but sets time to 23:59:59.999. Maybe better to set seconds to 59.000 so `... Month, 1)) - 1000)`. :-) – RobG Jun 14 '14 at 00:51
-
3I can't imagine why you'd want to leave 999ms out of a range calculation – jcollum Mar 17 '17 at 15:01
-
Same, the GMT is not conserved and you will get weird results when manipulating the created date object, see csukcc answer for a correct implementation – Tofandel Sep 17 '19 at 21:25
32
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));

Chetan S
- 23,637
- 2
- 63
- 78
-
1No need for two Date objects: `t.setMonths(t.getMonth()+1, 0, 23, 59, 59, 0)`. – RobG Jun 14 '14 at 00:55
-
1
-
7
var d = new Date();
console.log(d);
d.setMonth(d.getMonth() + 1); // set month as next month
console.log(d);
d.setDate(0); // get the last day of previous month
console.log(d);
Here is output from the code above:
Thu Oct 03 2013 11:34:59 GMT+0100 (GMT Daylight Time)
Sun Nov 03 2013 11:34:59 GMT+0000 (GMT Standard Time)
Thu Oct 31 2013 11:34:59 GMT+0000 (GMT Standard Time)

S M Sajjad Salam
- 21
- 8

csukcc
- 752
- 7
- 8
-
You could simplify this a little more by passing the date to `setMonth` like so: `d.setMonth(d.getMonth() + 1, 0)` – Jared Sep 20 '16 at 23:51
-
1This doesnt set the timestamp "11:59:59 PM" as requested in the question. – johnw182 Nov 22 '22 at 20:51
2
var d = new Date();
m = d.getMonth(); //current month
y = d.getFullYear(); //current year
alert(new Date(y,m,1)); //this is first day of current month
alert(new Date(y,m+1,0)); //this is last day of current month

jare25
- 506
- 1
- 7
- 17
-
I've tried this, and `alert(new Date(y,m+1,0));` gives you the last day for the next month. – mickburkejnr Jan 15 '14 at 12:05
-
1@mickburkejnr you may want to check again, I just tried it and it works as expected. – Jay Dadhania Sep 12 '18 at 05:07
1
Last day of the month
now = new Date
lastDayOfTheMonth = new Date(1900+now.getYear(), now.getMonth()+1, 0)

M.A.K. Ripon
- 2,070
- 3
- 29
- 47

david
- 39
- 2
- 10
1
var month = 1; // 1 for January
var d = new Date(2015, month, 0);
console.log(d); // last day in January

Sajin M Aboobakkar
- 4,051
- 4
- 30
- 39
1
Sometimes all you have is a text version of the current month, ie: April 2017.
//first and last of the current month
var current_month = "April 2017";
var arrMonth = current_month.split(" ");
var first_day = new Date(arrMonth[0] + " 1 " + arrMonth[1]);
//even though I already have the values, I'm using date functions to get year and month
//because month is zero-based
var last_day = new Date(first_day.getFullYear(), first_day.getMonth() + 1, 0, 23, 59, 59);
//use moment,js to format
var start = moment(first_day).format("YYYY-MM-DD");
var end = moment(last_day).format("YYYY-MM-DD");

Nicolas Giszpenc
- 677
- 6
- 11
0
Most of these answers are missing one thing or another. After playing with most of them I came up with the following that gives you the last possible millisecond of the month.
let testDate = new Date();
console.log(getMonthEnd(testDate));
function getMonthEnd(value) {
return new Date(value.getFullYear(), value.getMonth() + 1, 0, 23, 59, 59, 999);
}
Probably missing something in this one too but seems to cover all my requirements.

johnw182
- 1,349
- 1
- 16
- 22
0
I am using this code to get the last day of the current month
getLastDayOfMonth() {
const date = new Date()
const month = date.getMonth() + 1
const year = date.getFullYear()
return new Date(year, month, 0).getDate()
}

Tyler2P
- 2,324
- 26
- 22
- 31
-1
Calendar cal = Calendar.getInstance();
cal.set(Calendar.DATE, cal.getActualMaximum(Calendar.DATE));
Date lastDayOfMonth = cal.getTime();

yetAnotherSE
- 3,178
- 6
- 26
- 33
-
9
-
The question is about javascript, not Java! This answer should be removed. – Jay Dadhania Sep 12 '18 at 05:04
-1
Do not forget month started with 0 so +1 in month too.
let enddayofmonth = new Date(year, month, 0).getDate();

Infomaster
- 793
- 7
- 8