[removed] how can I increment a date string (YYYY-

2020-02-26 02:07发布

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?

7条回答
贪生不怕死
2楼-- · 2020-02-26 02:34

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

Has lots of functions for handling this kind of thing.

查看更多
Evening l夕情丶
3楼-- · 2020-02-26 02:35

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.

查看更多
等我变得足够好
4楼-- · 2020-02-26 02:35

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.

查看更多
霸刀☆藐视天下
5楼-- · 2020-02-26 02:40

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;
}
查看更多
ら.Afraid
6楼-- · 2020-02-26 02:44

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'
查看更多
够拽才男人
7楼-- · 2020-02-26 02:52

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.

查看更多
登录 后发表回答