Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

javascript - 为JavaScript日期添加天数(Add days to JavaScript Date)

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

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

1 Reply

0 votes
by (71.8m points)

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视为可变类而不是不变结构是合适的。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...