views:

47

answers:

3

I need to increment a date value by one day in Javascript. For example, I have a date value 2010-09-11 and I need to store the next date in a Javascript variable.

How can I increment a date by 1 day?

+3  A: 

Two options for you:

Raw JavaScript:

var today = new Date();
var tomorrow = new Date(today.getTime() + (24 * 60 * 60 * 1000));

Edit: See also org.life.java's answer and David's comment below: var tomorrow = new Date(); tomorrow.setDate(tomorrow.getDate() + 1);

Or using DateJS:

var today = new Date(); // Or Date.today()
var tomorrow = today.add(1).day();
T.J. Crowder
+1, although I usually go with a version of your first example, that's basically the same, although to me a bit more semantic: `var tomorrow = new Date(); tomorrow.setDate(tomorrow.getDate() + 1);`
David Hedlund
But I have date 2010-09-11 in a variable by using document.getElementById("arrival_date").value,it shows invalid date
santanu
To be picky, T.J., the value needs to be assigned by `setDate`, and not by mere integer incrementation. The value of `tomorrow` in your updated example would simply be `10` (where today is 9th). It is also crucial that the *initial* value of `tomorrow` when `setDate` is called, is the same as `today`, because you're only altering the day-part, not year/month. Basically, we're saying "take the date we've already got, but make its day 10 instead of 9.", and we get the added benefit of it being a date object, so it'll automatically correct for incrementing to 32, say.
David Hedlund
@David: Thanks, fixed.
T.J. Crowder
@santanu: That's a completely different problem: Date parsing (getting a date by interpreting a string). If that format is static, you can parse it yourself and feed the results into the `Date` constructor (`new Date(year, month, date)`); see section 15.9.2.1 of the [specification](http://www.ecma-international.org/publications/standards/Ecma-262.htm); if you want to accept a range of formats, definitely take a look at [DateJS](http://www.datejs.com).
T.J. Crowder
@santanu: Just to follow-on the above: Some browsers may successfully parse that format with `Date.parse` (which returns a number of milliseconds you can then feed into `new Date(ms)`). But beware, that can vary from browser to browser. Until recently, implementations could do just about anything they wanted to in `Date.parse`. The new 5th edition specification now has a minimum format that must be accepted, but I wouldn't count on that yet.
T.J. Crowder
+2  A: 
var myDate = new Date();

//add a day to the date
myDate.setDate(myDate.getDate() + 1);
org.life.java
+1 very nice...
T.J. Crowder
But I have date 2010-09-11 in a variable by using document.getElementById("arrival_date").value,it shows invalid date
santanu
@sanatu: In that case you need to format the string in a way that the browser will accept. Chrome accepts `new Date("2009-09-11")` but Firefox doesn't. I think most browsers accept `new Date("2009/09/11")` so an easy solution would be `var arrivalDate = document.getElementById('arival_date').value; var dayAfter = new Date(arrival_date.replace(/-/g, '/')); dayAfter.setDate(dayAfter.getDate()+1);`.
David Hedlund
A more solid approach than replacing `-` with `/` and hoping that browsers will accept it, would be to divide the string into parts: `var dateParts = arrivalDate.split('-'); var y = parseInt(dateParts[0], 10); var m = parseInt(dateParts[1], 10); var d = parseInt(dateParts[2], 10); var dayAfter = new Date(y, m-1, d+1);` Note that we're using `m-1` here, because javascript months are enum values (January being 0, not 1), and `d+1` because we're doing the incrementation already in the initial assignment (it'll automatically correct for Feb 29th, etc.)
David Hedlund
A: 

You first need to parse your string before following the other people's suggestion:

var dateString = "2010-09-11";
var myDate = new Date(dateString);

//add a day to the date
myDate.setDate(myDate.getDate() + 1);

If you want it back in the same format again you will have to do that "manually":

var y = myDate.getFullYear(),
    m = myDate.getMonth() + 1, // january is month 0 in javascript
    d = myDate.getDate();
var pad = function(val) { var str = val.toString(); return (str.length < 2) ? "0" + str : str};
dateString = [y, pad(m), pad(d)].join("-");

But I suggest getting Date.js as mentioned in other replies, that will help you alot.

ormuriauga