How can I obtain the last day of the month with the timestamp being 11:59:59 PM?
Asked
Active
Viewed 5.6k times
9 Answers
38
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
- 172,072
- 46
- 300
- 307
-
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
30
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,167
- 2
- 62
- 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
- 662
- 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
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
- 488
- 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
- 1,800
- 3
- 27
- 45
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
- 3,673
- 3
- 28
- 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
- 619
- 6
- 11
-1
Calendar cal = Calendar.getInstance();
cal.set(Calendar.DATE, cal.getActualMaximum(Calendar.DATE));
Date lastDayOfMonth = cal.getTime();
yetAnotherSE
- 2,948
- 6
- 25
- 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
- 565
- 4
- 6