How to add days to current Date
using JavaScript.
(如何使用JavaScript将天添加到当前Date
。)
AddDay
? (JavaScript是否具有诸如.Net的AddDay
类的内置函数?)
How to add days to current Date
using JavaScript.
(如何使用JavaScript将天添加到当前Date
。)
AddDay
? (JavaScript是否具有诸如.Net的AddDay
类的内置函数?)
You can create one with:-
(您可以使用以下方法创建一个:)
Date.prototype.addDays = function(days) { var date = new Date(this.valueOf()); date.setDate(date.getDate() + days); return date; } var date = new Date(); alert(date.addDays(5));
This takes care of automatically incrementing the month if necessary.
(这样可以在必要时自动增加月份。)
For example:(例如:)
8/31 + 1 day will become 9/1 .
(8/31 + 1天将变为9/1 。)
The problem with using setDate
directly is that it's a mutator and that sort of thing is best avoided.
(直接使用setDate
的问题在于它是一个mutator,最好避免这种事情。)
Date
as a mutable class rather than an immutable structure. (ECMA认为将Date
视为可变类而不是不变结构是合适的。)