我正在寻找最简单、最干净的方法将X个月添加到JavaScript日期中。

我宁愿不处理一年的滚动,也不愿意自己写函数。

有什么内置的东西可以做到这一点吗?


当前回答

来自@bmpsini和@Jazaret的回复,但没有扩展原型:使用普通函数(为什么扩展本机对象是一种糟糕的做法?)

function isLeapYear(year) { 
    return (((year % 4 === 0) && (year % 100 !== 0)) || (year % 400 === 0)); 
}

function getDaysInMonth(year, month) {
    return [31, (isLeapYear(year) ? 29 : 28), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][month];
}

function addMonths(date, value) {
    var d = new Date(date),
        n = date.getDate();
    d.setDate(1);
    d.setMonth(d.getMonth() + value);
    d.setDate(Math.min(n, getDaysInMonth(d.getFullYear(), d.getMonth())));
    return d;
}

使用它:

var nextMonth = addMonths(new Date(), 1);

其他回答

容易的,简单的

function addMonths(date, months) {date.setMonth(date.getMonth() + months); return date;};

使用它作为

 alert(new Date().toLocaleString()); //will say today
 alert(addMonths(new Date(),12).toLocaleString()); //will say next year, same day and month
d = new Date();

alert(d.getMonth()+1);

月份有一个基于0的指数,它应该警报(4),这是5(五月);

考虑到这些答案中没有一个会在月份变化时解释当年的情况,你可以在下面找到我做的一个答案,它应该可以处理这个问题:

方法:

Date.prototype.addMonths = function (m) {
    var d = new Date(this);
    var years = Math.floor(m / 12);
    var months = m - (years * 12);
    if (years) d.setFullYear(d.getFullYear() + years);
    if (months) d.setMonth(d.getMonth() + months);
    return d;
}

用法:

return new Date().addMonths(2);

我写了另一种解决方案,对我来说很好。当您希望计算合同的结束时,它很有用。例如,start=2016-01-15, months=6, end=2016-7-14(即最后一天-1):

<script>
function daysInMonth(year, month)
{
    return new Date(year, month + 1, 0).getDate();
}

function addMonths(date, months)
{
    var target_month = date.getMonth() + months;
    var year = date.getFullYear() + parseInt(target_month / 12);
    var month = target_month % 12;
    var day = date.getDate();
    var last_day = daysInMonth(year, month);
    if (day > last_day)
    {
        day = last_day;
    }
    var new_date = new Date(year, month, day);
    return new_date;
}

var endDate = addMonths(startDate, months);
</script>

例子:

addMonths(new Date("2016-01-01"), 1); // 2016-01-31
addMonths(new Date("2016-01-01"), 2); // 2016-02-29 (2016 is a leap year)
addMonths(new Date("2016-01-01"), 13); // 2017-01-31
addMonths(new Date("2016-01-01"), 14); // 2017-02-28

下面的函数在JavaScript中为日期添加月份(源代码)。它考虑了年的滚动和不同的月份长度:

function addMonths(date, months) { var d = date.getDate(); date.setMonth(date.getMonth() + +months); if (date.getDate() != d) { date.setDate(0); } return date; } // Add 12 months to 29 Feb 2016 -> 28 Feb 2017 console.log(addMonths(new Date(2016,1,29),12).toString()); // Subtract 1 month from 1 Jan 2017 -> 1 Dec 2016 console.log(addMonths(new Date(2017,0,1),-1).toString()); // Subtract 2 months from 31 Jan 2017 -> 30 Nov 2016 console.log(addMonths(new Date(2017,0,31),-2).toString()); // Add 2 months to 31 Dec 2016 -> 28 Feb 2017 console.log(addMonths(new Date(2016,11,31),2).toString());

上面的解决方案涵盖了从一个月移动的天数多于目标月份的边缘情况。如。

增加12个月至2020年2月29日(应该是2021年2月28日) 增加一个月至2020年8月31日(应该是2020年9月30日)

如果在应用setMonth时,月份的日期发生了变化,那么由于月份长度的差异,我们知道已经溢出到下一个月。在本例中,我们使用setDate(0)返回到上个月的最后一天。

注意:这个答案的这个版本取代了之前的版本(下面),之前的版本没有优雅地处理不同的月份长度。

var x = 12; //or whatever offset
var CurrentDate = new Date();
console.log("Current date:", CurrentDate);
CurrentDate.setMonth(CurrentDate.getMonth() + x);
console.log("Date after " + x + " months:", CurrentDate);