将天数添加到JavaScript日期对象,并增加月数 [英] Add days to Javascript Date object, and also increment month
问题描述
查看先前的问题和答案似乎应该这样:
Looking over the previous questions and answers it appeared this should work :
var palindrome = new Date('2011-11-11');
var december = new Date('2011-11-11');
december.setDate(palindrome.getDate()+20);
//should be december, but in fact loops back over to Nov 1st)
my jsFiddle
是否有一种简单的方法确保月份正确增加,还是我错过了明显的事情?
is there a simple way to ensure that months are incremented correctly, or have I missed something obvious ?
推荐答案
getMonth()
调用返回0到11之间的值,其中0是1月,11是12月,因此10表示11月。在字符串中使用该值时,需要将其值增加1。如果仅将其输出为字符串,则会看到它具有正确的日期。注意我还必须更改开始日期格式。它似乎不喜欢 2011-11-11
,所以我做到了 11/11/2011
。 http://jsfiddle.net/9HLSW/
The getMonth()
call returns a value between 0 and 11, where 0 is January and 11 is December, so 10 means November. You need to increment the value by 1 when using it in a string. If you simply output it as a string you'll see that it has the correct date. Note I also had to change the starting date format. It didn't seem to like 2011-11-11
so I made it 11/11/2011
. http://jsfiddle.net/9HLSW/
这篇关于将天数添加到JavaScript日期对象,并增加月数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!