如何获得该月的最后一天

Rod*_*Rod 16 javascript jquery

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

mik*_*iku 36

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

console.log(LastDayOfMonth(2009, 11))
Run Code Online (Sandbox Code Playgroud)

例:

> LastDayOfMonth(2009, 11)
Mon Nov 30 2009 23:59:59 GMT+0100 (CET)
Run Code Online (Sandbox Code Playgroud)

  • 我无法想象你为什么要离开范围计算999ms (3认同)

Che*_*try 24

这将给你当月的最后一天.

var t= new Date();
alert(new Date(t.getFullYear(), t.getMonth() + 1, 0, 23, 59, 59));
Run Code Online (Sandbox Code Playgroud)


小智 6

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);
Run Code Online (Sandbox Code Playgroud)

以下是上述代码的输出:
Thu Oct 03 2013 11:34:59 GMT + 0100(GMT Daylight Time)
Sun Nov 03 2013 2013 11:34:59 GMT + 0000(GMT Standard Time)
Thu Oct 31 2013 11:34: 59 GMT + 0000(格林威治标准时间标准时间)