Calculate last day of month in JavaScript

2019-01-01 12:42发布

If you provide 0 as the dayValue in Date.setFullYear you get the last day of the previous month:

d = new Date(); d.setFullYear(2008, 11, 0); //  Sun Nov 30 2008

There is reference to this behaviour at mozilla. Is this a reliable cross-browser feature or should I look at alternative methods?

15条回答
后来的你喜欢了谁
2楼-- · 2019-01-01 13:00

set month you need to date and then set the day to zero ,so month begin in 1 - 31 in date function then get the last day^^

var last = new Date(new Date(new Date().setMonth(7)).setDate(0)).getDate();
console.log(last);
查看更多
旧人旧事旧时光
3楼-- · 2019-01-01 13:03

I find this to be the best solution for me. Let the Date object calculate it for you.

var today = new Date();
var lastDayOfMonth = new Date(today.getFullYear(), today.getMonth()+1, 0);

Setting day parameter to 0 means one day less than first day of the month which is last day of the previous month.

查看更多
无与为乐者.
4楼-- · 2019-01-01 13:07
var month = 0; // January
var d = new Date(2008, month + 1, 0);
alert(d); // last day in January

IE 6: Thu Jan 31 00:00:00 CST 2008
IE 7: Thu Jan 31 00:00:00 CST 2008
IE 8: Beta 2: Thu Jan 31 00:00:00 CST 2008
Opera 8.54: Thu, 31 Jan 2008 00:00:00 GMT-0600
Opera 9.27: Thu, 31 Jan 2008 00:00:00 GMT-0600
Opera 9.60: Thu Jan 31 2008 00:00:00 GMT-0600
Firefox 2.0.0.17: Thu Jan 31 2008 00:00:00 GMT-0600 (Canada Central Standard Time)
Firefox 3.0.3: Thu Jan 31 2008 00:00:00 GMT-0600 (Canada Central Standard Time)
Google Chrome 0.2.149.30: Thu Jan 31 2008 00:00:00 GMT-0600 (Canada Central Standard Time)
Safari for Windows 3.1.2: Thu Jan 31 2008 00:00:00 GMT-0600 (Canada Central Standard Time)

Output differences are due to differences in the toString() implementation, not because the dates are different.

Of course, just because the browsers identified above use 0 as the last day of the previous month does not mean they will continue to do so, or that browsers not listed will do so, but it lends credibility to the belief that it should work the same way in every browser.

查看更多
查无此人
5楼-- · 2019-01-01 13:10

I would use an intermediate date with the first day of the next month, and return the date from the previous day:

int_d = new Date(2008, 11+1,1);
d = new Date(int_d - 1);
查看更多
千与千寻千般痛.
6楼-- · 2019-01-01 13:13

This works for me. Will provide last day of given year and month:

var d = new Date(2012,02,0);
var n = d.getDate();
alert(n);
查看更多
琉璃瓶的回忆
7楼-- · 2019-01-01 13:14

Below function gives the last day of the month :

function getLstDayOfMonFnc(date)
{
    return new Date(date.getFullYear(), date.getMonth(), 0).getDate()
}

console.log(getLstDayOfMonFnc(new Date(2016, 2, 15)))   // Output : 29
console.log(getLstDayOfMonFnc(new Date(2017, 2, 15)))   // Output : 28
console.log(getLstDayOfMonFnc(new Date(2017, 11, 15)))  // Output : 30
console.log(getLstDayOfMonFnc(new Date(2017, 12, 15)))  // Output : 31

Similarly we can get first day of the month :

function getFstDayOfMonFnc(date)
{
    return new Date(date.getFullYear(), date.getMonth(), 1).getDate()
}

console.log(getFstDayOfMonFnc(new Date(2016, 2, 15)))   // Output : 1
查看更多
登录 后发表回答