1

Looking for a simple solution for adding 2 days to a selected date in JQuery/Javascript.

The date is returned in the format Y-m-d producing for today for example 2011-07-09. What I want to do is add 2 days to that. This would be easy in php with the strtotime function, but how is it done in javascript.

Any ideas?

Marvellous

Walrus
  • 19,801
  • 35
  • 121
  • 199

3 Answers3

13

Use the JavaScript Date object's setDate() method.

var myDate = new Date('2011-07-09');
myDate.setDate(myDate.getDate() + 2);
Dan Herbert
  • 99,428
  • 48
  • 189
  • 219
  • I followed it with alert(myDate) and it says invalid Date as the alert. Have I messed something – Walrus Jul 09 '11 at 13:08
  • @Robin When I run the above code, I get `Mon Jul 11 2011 00:00:00 GMT-0400 (Eastern Daylight Time)` as the output in an alert. Are you sure you're feeding it a valid date? What browser are you using? – Dan Herbert Jul 09 '11 at 21:53
  • Problem was that it only worked in standard format in firefox. Alternative found but safari considers this an invalid date – Walrus Jul 12 '11 at 19:25
  • @Robin One option you may want to consider trying (if it isn't overkill in your app) would be to use the library [DateJS](http://www.datejs.com/). It has some useful date manipulation utilities that would make these types of operations more smooth. – Dan Herbert Jul 14 '11 at 00:20
  • setDate() sets the day of the month in the existing date. Is it well defined if you call .setDate(33)? – Dan Jun 04 '13 at 03:33
  • @me: yup, it's valid. There's a note in the MDN docs saying it will try to do what you'd expect: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/setDate and the spec backs it up: http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf (see last step of MakeDay procedure) – Dan Jun 04 '13 at 03:59
3

If your date is a JavaScript Date object, then you can do something like this:

var myDate = new Date();
var newDate = new Date(myDate.getTime() + 2 * 24 * 60 * 60 * 1000);

Once you have added the days to your date, you can then format it however you have done it currently.

Obviously, you could make that shorter if you condense all the multiplications, but it's more obvious what's actually happening that way.

James Allardice
  • 164,175
  • 21
  • 332
  • 312
3

The same problem, I've created following function for basic functionality. It accepts and returns time similar to PHP, integer number of milliseconds since 1970-01-01.

var d = new Date(2011, 0, 1, 0, 0, 0, 0);
var result = strtotime('-66 second', d.getTime());

function strtotime(time, now) {

  var d = new Date();
  d.setTime(now);

  var ParsedTime = new RegExp('([+-][0-9]+) (\\w+)', 'i').exec(time);
  if(!ParsedTime) return 0;

  switch(ParsedTime[2]) {
    case 'second':
      d.setSeconds(d.getSeconds() + parseInt(ParsedTime[1], 10));
      break;
    case 'minute':
      d.setMinutes(d.getMinutes() + parseInt(ParsedTime[1], 10));
      break;
    case 'hour':
      d.setHours(d.getHours() + parseInt(ParsedTime[1], 10));
      break;
    case 'day':
      d.setDate(d.getDate() + parseInt(ParsedTime[1], 10));
      break;
    case 'month':
      d.setMonth(d.getMonth() + parseInt(ParsedTime[1], 10));
      break;
    case 'year':
      d.setFullYear(d.getFullYear() + parseInt(ParsedTime[1], 10));
      break;
  }

  return d.getTime();
}
brubla
  • 61
  • 2