Reputation: 700
I created this function to increment a date (for a 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:
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?
Upvotes: 3
Views: 25888
Reputation: 135862
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?
Some points you must consider:
new Date(2013, 7, 3)
does not create the date 03/Jul/2013:
7
actually is August.moreDay.setDate(act.getDate()+1)
does not make moreDay
the date act
plus one day. Because:
date.getDate()
returns the day of the month of the date
variabledate.setDate(int)
sets the day of date
(just the day, leaving the year and month intact)moreDay.setDate(act.getDate()+1)
does then is:
moreDay
's day the value of act
's day plus one.In other words, as moreDay
's value is new Date()
(which is the current day - right now 11/Jun/2013 where I live), and act
's day is 3
, the statement evaluation then really is:
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.
Upvotes: 14
Reputation: 4224
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)
Upvotes: 0
Reputation: 22637
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
.
This alternative should be faster than setDate
:
moreDay.setTime(moreDay.getTime() + 864e5);
Or you could define your Date
object directly with an added day:
moreDay = new Date(decomposed[2], decomposed[1] - 1, +decomposed[0] + 1);
Upvotes: 7
Reputation: 59363
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);
}
Upvotes: 5