Rod
Rod

Reputation: 15423

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?

Upvotes: 19

Views: 66956

Answers (11)

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()
}

Upvotes: 0

johnw182
johnw182

Reputation: 1469

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.

Upvotes: 0

david
david

Reputation: 39

Last day of the month

now = new Date
lastDayOfTheMonth = new Date(1900+now.getYear(), now.getMonth()+1, 0)

Upvotes: 1

Infomaster
Infomaster

Reputation: 873

Do not forget month started with 0 so +1 in month too.

let enddayofmonth = new Date(year, month, 0).getDate();

Upvotes: -1

csukcc
csukcc

Reputation: 772

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)

Upvotes: 7

miku
miku

Reputation: 188004

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)

Upvotes: 41

Nicolas Giszpenc
Nicolas Giszpenc

Reputation: 703

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");

Upvotes: 1

Sajin M Aboobakkar
Sajin M Aboobakkar

Reputation: 4229

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

Upvotes: 1

jare25
jare25

Reputation: 516

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   

Upvotes: 2

yetAnotherSE
yetAnotherSE

Reputation: 3248

Calendar cal = Calendar.getInstance();
cal.set(Calendar.DATE, cal.getActualMaximum(Calendar.DATE));

Date lastDayOfMonth = cal.getTime();

Upvotes: -1

Chetan S
Chetan S

Reputation: 23803

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));

Upvotes: 33

Related Questions