如何获得该月的最后一天

时间:2009-12-17 21:46:53

标签: javascript jquery

如何获取当月的最后一天,时间戳是晚上11:59:59?

9 个答案:

答案 0 :(得分:35)

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

console.log(LastDayOfMonth(2009, 11))

示例:

> LastDayOfMonth(2009, 11)
Mon Nov 30 2009 23:59:59 GMT+0100 (CET)

答案 1 :(得分:24)

这将为您提供当月的最后一天。

var t= new Date();
alert(new Date(t.getFullYear(), t.getMonth() + 1, 0, 23, 59, 59));

答案 2 :(得分:5)



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




以下是上述代码的输出:
2013年10月3日星期四11:34:59 GMT + 0100(GMT日光时间)
Sun Nov 03 2013 11:34:59 GMT + 0000(GMT标准时间)
2013年10月31日星期四11:34:59 GMT + 0000(GMT标准时间)

答案 3 :(得分: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   

答案 4 :(得分:1)

该月的最后一天

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

http://coffeescriptcookbook.com/chapters/dates_and_times/finding-last-day-of-the-month

答案 5 :(得分:1)

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

答案 6 :(得分:1)

有时你只拥有本月的文字版本,即:2017年4月。

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

答案 7 :(得分:0)

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

Date lastDayOfMonth = cal.getTime();

答案 8 :(得分:-1)

别忘了月份从0开始,所以月份也为+1。

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