-2

I got this from another stack question

incr_date(date_str){
    let parts = date_str.split("-");
    let 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("-");
}

It works but how can I convert this function to decrement the date instead of increment?

I'm doing this on a react native component so I dont want to import any javascript libraries like moment.js

Thomas Charlesworth
  • 1,789
  • 5
  • 28
  • 53
  • So you're recreating a date library? – Isaac Mar 31 '17 at 04:36
  • 3
    change the first `+ 1` to `- 1` – Ry- Mar 31 '17 at 04:37
  • Just subtract that day instead of adding it? – Bergi Mar 31 '17 at 04:38
  • @Isaac—no, the OP just wants a very simple function, but could do with reading posts like [*Where can I find documentation on formatting a date in JavaScript?*](http://stackoverflow.com/questions/1056728/where-can-i-find-documentation-on-formatting-a-date-in-javascript?s=1|14.0161) ;-) – RobG Mar 31 '17 at 05:00
  • There are very much simpler solutions to adding one to a date, e.g. [*Add +1 to current date*](http://stackoverflow.com/questions/9989382/add-1-to-current-date). This function mixes parsing, adding and formatting in one function, which is less than optimal. – RobG Mar 31 '17 at 05:02

2 Answers2

0

function dateAdd(dte){
var date = new Date(dte);
date.setDate(date.getDate() + 1);
console.log("add one day= "+date)
}
function datesub(dte){
var date = new Date(dte);
date.setDate(date.getDate() - 1);
console.log("minus one day = "+ date)
}
dateAdd("01-01-2017")
datesub("01-01-2017")
Karan Singh
  • 876
  • 1
  • 6
  • 12
0

I'd convert the string to Javascript understandable format, increment a day and convert it back to user understandable format. I'm using the flag(Boolean) to determine weather to Increment the date and vice versa.

var convertDate = function(dt, flag) {
  var dateArr = dt.split('-');
  var tempDate = new Date();
  var mm = dateArr[1] - 1; //Javascript considers 0 as Jan
  tempDate.setFullYear(dateArr[0]);
  tempDate.setMonth(mm);
  tempDate.setDate(dateArr[2]);
  if (flag) {
    tempDate.setDate(tempDate.getDate(dateArr[2]) + 1);//Add's one day
  } else {
    tempDate.setDate(tempDate.getDate(dateArr[2]) - 1);//Sub's one day
  } 
  var userFriendlyMonth = (Number(tempDate.getMonth()) + 1); //user considers 1 as Jan
  return tempDate.getFullYear() + '-' + userFriendlyMonth + '-' + tempDate.getDate();
}
document.getElementById("increment").innerHTML = convertDate('2018-11-30', true);
document.getElementById("decrement").innerHTML = convertDate('2018-11-30', false);
<div>Increment: <span id="increment"></span></div>
<div>Decrement: <span id="decrement"></span></div>
V Reddy
  • 38
  • 1
  • 5