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

Categories

How to add days to current Date using JavaScript.

(如何使用JavaScript将天添加到当前Date 。)

Does JavaScript have a built in function like .Net's AddDay ?

(JavaScript是否具有诸如.Net的AddDay类的内置函数?)

  ask by Ashesh translate from so

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

1 Answer

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,最好避免这种事情。)

ECMA saw fit to treat Date as a mutable class rather than an immutable structure.

(ECMA认为将Date视为可变类而不是不变结构是合适的。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...