如何在 JavaScript 中为日期添加月份?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5645058/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 18:14:43  来源:igfitidea点击:

How to add months to a date in JavaScript?

javascriptdate

提问by Kanak Vaghela

I want to add months to a date in JavaScript.

我想在 JavaScript 中为日期添加月份。

For example: I am inserting date 06/01/2011(format mm/dd/yyyy) and now I want to add 8 months to this date. I want the result to be 02/01/2012.

例如:我正在插入日期06/01/2011(格式mm/dd/yyyy),现在我想在这个日期上加上 8 个月。我希望结果是02/01/2012.

So when adding months, the year may also increase.

所以当增加月份时,年份也可能增加。

回答by Daniel Protopopov

Corrected as of 25.06.2019:

2019 年 6 月 25 日更正:

var newDate = new Date(date.setMonth(date.getMonth()+8));

OldFrom here:

这里

var jan312009 = new Date(2009, 0, 31);
var eightMonthsFromJan312009  = jan312009.setMonth(jan312009.getMonth()+8);

回答by mu is too short

Split your date into year, month, and day components then use Date:

将您的日期拆分为年、月和日组件,然后使用Date

var d = new Date(year, month, day);
d.setMonth(d.getMonth() + 8);

Date will take care of fixing the year.

日期将负责确定年份。

回答by Jazaret

I took a look at the datejs and stripped out the code necessary to add months to a date handling edge cases (leap year, shorter months, etc):

我查看了 datejs 并删除了将月份添加到处理边缘情况(闰年、较短月份等)的日期所需的代码:

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

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

Date.prototype.isLeapYear = function () { 
    return Date.isLeapYear(this.getFullYear()); 
};

Date.prototype.getDaysInMonth = function () { 
    return Date.getDaysInMonth(this.getFullYear(), this.getMonth());
};

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

This will add "addMonths()" function to any javascript date object that should handle edge cases. Thanks to Coolite Inc!

这会将“addMonths()”函数添加到任何应该处理边缘情况的 javascript 日期对象。感谢 Coolite 公司!

Use:

用:

var myDate = new Date("01/31/2012");
var result1 = myDate.addMonths(1);

var myDate2 = new Date("01/31/2011");
var result2 = myDate2.addMonths(1);

->> newDate.addMonths -> mydate.addMonths

->> newDate.addMonths -> mydate.addMonths

result1 = "Feb 29 2012"

结果 1 =“2012 年 2 月 29 日”

result2 = "Feb 28 2011"

结果 2 =“2011 年 2 月 28 日”

回答by Alex

I would highly recommend taking a look at datejs. With it's api, it becomes drop dead simple to add a month (and lots of other date functionality):

我强烈建议看看datejs。有了它的 api,添加一个月(以及许多其他日期功能)变得非常简单:

var one_month_from_your_date = your_date_object.add(1).month();

What's nice about datejsis that it handles edge cases, because technically you can do this using the native Dateobject and it's attached methods. But you end up pulling your hair out over edge cases, which datejshas taken care of for you.

datejs它的优点在于它可以处理边缘情况,因为从技术上讲,您可以使用本机Date对象及其附加方法来做到这一点。但是你最终会把你的头发从边缘情况拉出来,这datejs已经为你照顾好了。

Plus it's open source!

另外它是开源的!