如果在Date中提供0作为dayValue。setFullYear你会得到上个月的最后一天:
d = new Date(); d.setFullYear(2008, 11, 0); // Sun Nov 30 2008
在mozilla中有这种行为的参考。这是一个可靠的跨浏览器功能吗?或者我应该看看其他的方法吗?
如果在Date中提供0作为dayValue。setFullYear你会得到上个月的最后一天:
d = new Date(); d.setFullYear(2008, 11, 0); // Sun Nov 30 2008
在mozilla中有这种行为的参考。这是一个可靠的跨浏览器功能吗?或者我应该看看其他的方法吗?
当前回答
这将给你当月的最后一天。
注:在ios设备上包含时间。 # gshoanganh
var date = new Date();
console.log(new Date(date.getFullYear(), date.getMonth() + 1, 0, 23, 59, 59));
其他回答
我将使用一个中间日期与下个月的第一天,并返回前一天的日期:
int_d = new Date(2008, 11+1,1);
d = new Date(int_d - 1);
我最近不得不做一些类似的事情,这是我想到的:
/**
* Returns a date set to the begining of the month
*
* @param {Date} myDate
* @returns {Date}
*/
function beginningOfMonth(myDate){
let date = new Date(myDate);
date.setDate(1)
date.setHours(0);
date.setMinutes(0);
date.setSeconds(0);
return date;
}
/**
* Returns a date set to the end of the month
*
* @param {Date} myDate
* @returns {Date}
*/
function endOfMonth(myDate){
let date = new Date(myDate);
date.setDate(1); // Avoids edge cases on the 31st day of some months
date.setMonth(date.getMonth() +1);
date.setDate(0);
date.setHours(23);
date.setMinutes(59);
date.setSeconds(59);
return date;
}
向它传递一个日期,它将返回一个设置为月初或月底的日期。
begninngOfMonth函数是相当不言自明的,但是endOfMonth函数中的内容是,我将这个月递增到下个月,然后使用setDate(0)将前一天回滚到上个月的最后一天,这是setDate规范的一部分:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/setDate https://www.w3schools.com/jsref/jsref_setdate.asp
然后我将小时/分/秒设置为一天的结束,这样如果您正在使用某种期望日期范围的API,您将能够捕获最后一天的全部内容。这部分内容可能超出了最初帖子的要求,但它可以帮助其他人寻找类似的解决方案。
编辑:如果您想要更加精确,还可以使用setMilliseconds()额外设置毫秒。
下面是一个保留GMT时间和初始日期时间的答案
var date = new Date(); var first_date = new Date(date); //Make a copy of the date we want the first and last days from first_date.setUTCDate(1); //Set the day as the first of the month var last_date = new Date(first_date); //Make a copy of the calculated first day last_date.setUTCMonth(last_date.getUTCMonth() + 1); //Add a month last_date.setUTCDate(0); //Set the date to 0, this goes to the last day of the previous month console.log(first_date.toJSON().substring(0, 10), last_date.toJSON().substring(0, 10)); //Log the dates with the format yyyy-mm-dd
如果你需要精确的以毫秒为单位的月底(例如时间戳):
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 ())
Var月= 0;/ / 1月 var d = new日期(2008,月份+ 1,0); console.log (d.toString ());// 1月的最后一天
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)
输出差异是由于toString()实现的差异,而不是由于日期不同。
当然,仅仅因为上面列出的浏览器使用0作为上个月的最后一天并不意味着它们会继续这样做,或者没有列出的浏览器会这样做,但它使人们相信它应该在每个浏览器中以相同的方式工作。