Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
273 views
in Technique[技术] by (71.8m points)

javascript - 如何在JavaScript中添加月份到日期? [重复](How to add months to a date in JavaScript? [duplicate])

This question already has an answer here:

(这个问题在这里已有答案:)

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.

(例如:我正在插入日期06/01/2011 (格式mm/dd/yyyy ),现在我想在这个日期添加8个月。)

I want the result to be 02/01/2012 .

(我希望结果是02/01/2012 。)

So when adding months, the year may also increase.

(因此,当添加月份时,年份也可能会增加。)

  ask by Kanak Vaghela translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Corrected as of 25.06.2019:

(截至25.06.2019已更正:)

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

Old From here :

(这里 :)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...