且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将天数添加到JavaScript日期对象,并增加月数

更新时间:2023-01-31 17:08:48

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/