简体   繁体   中英

javascript: how can I increment a date string (YYYY-MM-DD) by 1 day

I know how to do this in php with date() and mktime() functions, but have no idea how to accomplish the same thing in javascript...

function incr_date(date_str){
    //...magic here
    return next_date_str;
}

var date_str = '2011-02-28';
console.log( incr_date(date_str) ); //want to output "2011-03-01"

is this even possible with js?

First you parse it, then you use dt.setDate(dt.getDate() + 1) . You'll have to parse it manually, though, or using DateJS or similar; that format is not yet supported across all major browsers ( new Date(date_str) will not work reliably cross-browser; see note below). And then convert it back to your format.

Something along these lines:

function incr_date(date_str){
  var parts = date_str.split("-");
  var dt = new Date(
    parseInt(parts[0], 10),      // year
    parseInt(parts[1], 10) - 1,  // month (starts with 0)
    parseInt(parts[2], 10)       // date
  );
  dt.setDate(dt.getDate() + 1);
  parts[0] = "" + dt.getFullYear();
  parts[1] = "" + (dt.getMonth() + 1);
  if (parts[1].length < 2) {
    parts[1] = "0" + parts[1];
  }
  parts[2] = "" + dt.getDate();
  if (parts[2].length < 2) {
    parts[2] = "0" + parts[2];
  }
  return parts.join("-");
}

Live example

Note that setDate will correctly handle rolling over to the next month (and year if necessary).

Live example

The above is tested and works on IE6, IE7, IE8; Chrome, Opera, and Firefox on Linux; Chrome, Opera, Firefox, and Safari on Windows.

A note about support for this format in JavaScript: The new Date(string) constructor in JavaScript only recently had the formats that it would accept standardized, as of ECMAScript 5th edition released in December 2009. Your format will be supported when browsers support it, but as of this writing, no released version of IE (not even IE8) supports it. Other recent browsers mostly do.

If you are using jQuery & jQuery UI then use the following:

var dt = $.datepicker.parseDate('yy-mm-dd', '2011-02-25');
dt.setDate(dt.getDate() + 1)
var dtNew = $.datepicker.formatDate('yy-mm-dd', dt);

You can parse and format any way you want, and most importantly you will not need a new function in your JS Library.

First you need to turn your date string into a Date object.

var date = new Date('2011-02-28');

Then you need to add one to the day part of the date.

date.setDate(d.getDate() + 1);

Although this looks it might end up with the 29th of February, it won't and will give you the 1st March.

I modified this excellent answer https://stackoverflow.com/a/5116693/4021614 to handle both incrementing and decrementing by any amount (using this method https://stackoverflow.com/a/25114400/4021614 ).

function incrementDate(date_str, incrementor) {
    var parts = date_str.split("-");
    var dt = new Date(
        parseInt(parts[0], 10),      // year
        parseInt(parts[1], 10) - 1,  // month (starts with 0)
        parseInt(parts[2], 10)       // date
    );
    dt.setTime(dt.getTime() + incrementor * 86400000);
    parts[0] = "" + dt.getFullYear();
    parts[1] = "" + (dt.getMonth() + 1);
    if (parts[1].length < 2) {
        parts[1] = "0" + parts[1];
    }
    parts[2] = "" + dt.getDate();
    if (parts[2].length < 2) {
        parts[2] = "0" + parts[2];
    }
    return parts.join("-");
};

example:

//increment by 7 days
incrementDate('2015-01-01', 7);
//returns '2015-01-08'

//decrement by 1 day
incrementDate('2015-01-01', -1);
//returns '2014-12-31'

I would use date.js from here - http://www.datejs.com/

Has lots of functions for handling this kind of thing.

Yes. First you read the date and you convert to a date object, then you add 1 day and return the result

function incrementDate(date_str) {
    var date = new Date(date_str);
    date.setDate(date.getDate() + 1);
    return date.getFullYear() + '-' + date.getMonth() + '-' + date.getDate();
}

The Date object , which is native in javascript, takes care of all problems arising from leap years and so on.

i hope this will help

function incr_date(date_str,no_of_days_to_add){
 var futureDate=new Date(date_str);

  futureDate.setDate(new Date().getDate()+no_of_days_to_add);
  var next_date_str= futureDate;

    return next_date_str;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM