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

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

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


当前回答

你可以通过下面的代码获取当月的第一个和最后一个日期:

var dateNow = new Date();  
var firstDate = new Date(dateNow.getFullYear(), dateNow.getMonth(), 1);  
var lastDate = new Date(dateNow.getFullYear(), dateNow.getMonth() + 1, 0);

或者如果你想用自定义格式格式化日期,那么你可以使用moment js

var dateNow= new Date();  
var firstDate=moment(new Date(dateNow.getFullYear(),dateNow.getMonth(), 1)).format("DD-MM-YYYY");  
var currentDate = moment(new Date()).format("DD-MM-YYYY"); //to  get the current date var lastDate = moment(new
 Date(dateNow.getFullYear(), dateNow.getMonth() + 1, 0)).format("DD-MM-YYYY"); //month last date

其他回答

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

函数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))) //输出

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

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>

这个很好用:

Date.prototype.setToLastDateInMonth = function () {

    this.setDate(1);
    this.setMonth(this.getMonth() + 1);
    this.setDate(this.getDate() - 1);

    return this;
}

我最近不得不做一些类似的事情,这是我想到的:

/**
* 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()额外设置毫秒。