Javascript:setDate(DATE + day) 不更新月份
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17054693/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript: setDate(DATE + day) not update month
提问by Theraloss
I created this function to increment a date (for a jQuery UI Datepicker):
我创建了这个函数来增加一个日期(对于 jQuery UI Datepicker):
function addDay(date) {
var moreDay = new Date();
var decomposed = date.split("-");
var act = new Date(decomposed[2], decomposed[1], decomposed[0]);
moreDay.setDate(act.getDate()+1);
return moreDay;
}
So, it scomposes the date (ex: 12-06-2013 (dd-mm-YYYY)) and put the values into a new Date, after that it addes a day. It works, but the month not change. Example, I changed the function into this:
因此,它组合日期(例如:12-06-2013 (dd-mm-YYYY))并将这些值放入一个新的日期中,然后添加一天。它有效,但月份不变。例如,我将函数更改为:
function addDay() {
var moreDay = new Date();
var act = new Date(2013, 7, 3);
moreDay.setDate(act.getDate()+1);
alert(moreDay);
}
And it returns Jun 4th 2013.. How it's possible?
它返回 2013 年 6 月 4 日.. 怎么可能?
回答by acdcjunior
I think I see what's the trouble.
我想我明白有什么问题了。
function addDay() {
var moreDay = new Date();
var act = new Date(2013, 7, 3);
moreDay.setDate(act.getDate()+1);
alert(moreDay);
}
And it returns Jun 4th 2013.. How it's possible?
它返回 2013 年 6 月 4 日.. 怎么可能?
Some points you must consider:
您必须考虑的一些要点:
- The creation of the date
new Date(2013, 7, 3)
does notcreate the date 03/Jul/2013:- Months are zero-based, meaning they go from 0 to 11, not 1 to 12.
- This way, the month
7
actually is August.
- This way, the month
- Months are zero-based, meaning they go from 0 to 11, not 1 to 12.
- The statement
moreDay.setDate(act.getDate()+1)
does not makemoreDay
the dateact
plus one day. Because:date.getDate()
returns the dayof the monthof thedate
variabledate.setDate(int)
sets the dayofdate
(justthe day, leaving the year and month intact)- What the statement
moreDay.setDate(act.getDate()+1)
does then is:- Set
moreDay
's daythe value ofact
's day plus one.
- Set
- 日期的创作
new Date(2013, 7, 3)
并没有创建之日起03 /七月/ 2013:- 月份是从零开始的,这意味着它们从 0 到 11,而不是从 1 到 12。
- 这样,月份
7
实际上是八月。
- 这样,月份
- 月份是从零开始的,这意味着它们从 0 到 11,而不是从 1 到 12。
- 声明
moreDay.setDate(act.getDate()+1)
没有使moreDay
日期act
加一天。因为:date.getDate()
返回日当月的的date
变量date.setDate(int)
设置一天的date
(仅仅一天,留下的年份和月份完好)- 该语句的
moreDay.setDate(act.getDate()+1)
作用是:- 设置
moreDay
's day's day的值act
加一。
- 设置
In other words, as moreDay
's value is new Date()
(which is the current day - right now 11/Jun/2013where I live), and act
's day is 3
, the statement evaluationthen really is:
换句话说, asmoreDay
的值是new Date()
(即当前日期 - 现在是我居住的11/Jun/2013),而act
's day is 3
,那么语句评估实际上是:
Step #1: moreDay.setDate( act.getDate() + 1 );
Step #2: moreDay.setDate( (03/Aug/2013).getDate() + 1 );
Step #3: moreDay.setDate( 03 + 1 );
Step #4: moreDay.setDate( 4 );
Step #5: (11/Jun/2013).setDate( 4 );
Step #6: (04/Jun/2013)
Thus ending Jun 4th 2013.
从而于 2013 年 6 月 4 日结束。
回答by Doorknob
moreDay.setDate(act.getDate()+1);
This will only add one day. Why do you expect it to add a month, too?
这只会增加一天。为什么你期望它也增加一个月?
If you want to add a month also, try this:
如果你还想增加一个月,试试这个:
function addDay() {
var moreDay = new Date();
var act = new Date(2013, 7, 3);
moreDay.setMonth(act.getMonth()+1); // add one month
moreDay.setDate(act.getDate()+1); // add one day
alert(moreDay);
}
回答by MaxArt
It's not clear what you want to do with your function. If you want to take a date and add a day, try with this:
不清楚你想用你的函数做什么。如果您想约会并添加一天,请尝试以下操作:
function addDay(date) {
var decomposed = date.split("-"),
moreDay = new Date(decomposed[2], decomposed[1] - 1, decomposed[0]);
moreDay.setDate(moreDay.getDate() + 1);
alert(moreDay);
}
Please take note that in common dd-mm-yyyy dates, January is 1, whereas to define a Date
object January is 0, hence the - 1
.
请注意,在常见的 dd-mm-yyyy 日期中,一月是 1,而定义Date
对象一月是 0,因此- 1
.
This alternative should be faster than setDate
:
这种替代方法应该比setDate
:
moreDay.setTime(moreDay.getTime() + 864e5);
Or you could define your Date
object directly with an added day:
或者您可以Date
直接添加一天来定义您的对象:
moreDay = new Date(decomposed[2], decomposed[1] - 1, +decomposed[0] + 1);
回答by PHP Worm...
One way to do this:
一种方法:
var now = new Date();
function addDays(date,days) {
var currentDate = date.getDate();
date.setDate(currentDate + days);
alert(date);
}
addDays(now,14)