如果在Date中提供0作为dayValue。setFullYear你会得到上个月的最后一天:

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

在mozilla中有这种行为的参考。这是一个可靠的跨浏览器功能吗?或者我应该看看其他的方法吗?


当前回答

在计算机术语中,新的Date()和正则表达式解决方案很慢!如果您想要一个超级快速(并且超级神秘)的一行程序,可以试试这个(假设m的格式是Jan=1)。我不断尝试不同的代码更改以获得最佳性能。

我目前最快的版本是:

在使用位运算符(惊人的速度)查看闰年检查相关问题并发现25和15的神奇数字所代表的含义后,我提出了这个优化的混合答案(注意参数m和y显然必须是整数才能工作):

function getDaysInMonth(m, y) {
    return m===2 ? y & 3 || !(y%25) && y & 15 ? 28 : 29 : 30 + (m+(m>>3)&1);
}

考虑到位移位,这显然假设m和y参数都是整数,因为将数字作为字符串传递会导致奇怪的结果。

JSFiddle: http://jsfiddle.net/TrueBlueAussie/H89X3/22/

JSPerf结果:http://jsperf.com/days-in-month-head-to-head/5

由于某种原因,在几乎所有浏览器上(m+(m>>3)&1)都比(5546>>m&1)更有效。

唯一真正的速度竞争来自@GitaarLab,所以我创建了一个直接的JSPerf供我们测试:http://jsperf.com/days-in-month-head-to-head/5


它的工作基于我的闰年答案在这里:javascript找到闰年这个答案在这里闰年检查使用位运算符(惊人的速度)以及以下二进制逻辑。

用二进制月份快速学习一下:

如果您以二进制形式解释所需月份(Jan = 1)的索引,您会注意到有31天的月份要么有第3位清除和第0位设置,要么有第3位设置和第0位清除。

Jan = 1  = 0001 : 31 days
Feb = 2  = 0010
Mar = 3  = 0011 : 31 days
Apr = 4  = 0100
May = 5  = 0101 : 31 days
Jun = 6  = 0110
Jul = 7  = 0111 : 31 days
Aug = 8  = 1000 : 31 days
Sep = 9  = 1001
Oct = 10 = 1010 : 31 days
Nov = 11 = 1011
Dec = 12 = 1100 : 31 days

这意味着你可以用>> 3将值移动3个位置,用原来的^ m对位进行异或,并使用& 1查看位位置0的结果是1还是0。注意:结果是+比XOR(^)略快,(m >> 3) + m在位0中给出相同的结果。

JSPerf结果:http://jsperf.com/days-in-month-perf-test/6

其他回答

下面的函数给出了这个月的最后一天:

函数getLstDayOfMonFnc(date) { 返回new Date(Date . getfullyear (), Date . getmonth (), 0).getDate() } console.log(getLstDayOfMonFnc(new Date(2016, 2,15))) //输出:29 console.log(getLstDayOfMonFnc(new Date(2017,2,15))) //输出:28 console.log(getLstDayOfMonFnc(new Date(2017,11,15))) //输出:30 console.log(getLstDayOfMonFnc(new Date(2017,12,15))) //输出:31

类似地,我们可以得到这个月的第一天:

函数getFstDayOfMonFnc(日期){ 返回new Date(Date . getfullyear (), Date . getmonth (), 1).getDate() } console.log(getFstDayOfMonFnc(new Date(2016, 2,15))) //输出

我知道这只是一个语义问题,但我最终以这种形式使用它。

var lastDay = new Date(new Date(2008, 11+1,1) - 1).getDate();
console.log(lastDay);

因为函数是从内部参数向外解析的,所以工作原理是一样的。

然后,您可以用所需的详细信息替换年和月/年,无论它来自当前日期。或者一个特定的月/年。

我将使用一个中间日期与下个月的第一天,并返回前一天的日期:

int_d = new Date(2008, 11+1,1);
d = new Date(int_d - 1);

公认的答案不适合我,我是这样做的。

$( function() { $( "#datepicker" ).datepicker(); $('#getLastDateOfMon').on('click', function(){ var date = $('#datepicker').val(); // Format 'mm/dd/yy' eg: 12/31/2018 var parts = date.split("/"); var lastDateOfMonth = new Date(); lastDateOfMonth.setFullYear(parts[2]); lastDateOfMonth.setMonth(parts[0]); lastDateOfMonth.setDate(0); alert(lastDateOfMonth.toLocaleDateString()); }); }); <!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css"> <link rel="stylesheet" href="/resources/demos/style.css"> <script src="https://code.jquery.com/jquery-1.12.4.js"></script> <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script> </head> <body> <p>Date: <input type="text" id="datepicker"></p> <button id="getLastDateOfMon">Get Last Date of Month </button> </body> </html>

如果你需要精确的以毫秒为单位的月底(例如时间戳):

d = new Date() console.log (d.toString ()) d.setDate (1) d.setHours(23, 59, 59, 999) d.setMonth(d.getMonth() + 1) d.setDate(d.getDate() - 1) console.log (d.toString ())