2

I want to add a day to date I get from textbox.I tried the code below but it did not work

    function get_renew_date ()
    {
        var chkautorenew=document.getElementById("MainContent_chk_Isauto_Renew");
        if (chkautorenew.checked) {
            end_date = document.getElementById("MainContent_txtContract_End_date").value;
            renew_date = document.getElementById("MainContent_txtContract_Renew").value;
            dat = new Date(end_date.toDateString());
            //renew_date=dat.addDays(1);
            alert(end_date.toDateString());
        }
     }

I'm also worried about the client date format and the last day of the month, how can I be sure that after adding a day it will not be like this 32/03/2016

thank you

Daina Hodges
  • 823
  • 3
  • 12
  • 37
  • Have you tried `renew_date.setDate(end_date.getDate() + 1);` ? – John Mar 21 '16 at 16:01
  • @Roland that requires jquery, use vanilla js and stop being so dependent on jquery – John Mar 21 '16 at 16:04
  • If you are not opposed to libraries and don't like to reinvent the wheel, then moment.js is a fantastic date manipulation library: http://momentjs.com/. As it seems you are building some kind of contract management application, I imagine you will need a lot more date manipulation operations than just this one. – erictgrubaugh Mar 21 '16 at 16:34

3 Answers3

5

Try this

var date1 = new Date("03/31/2016");
var next_date = new Date(date1.getTime() + 86400000);
alert(next_date.toLocaleDateString());
Niklesh Raut
  • 34,013
  • 16
  • 75
  • 109
3

The behavior of setting a date is internal so if you set a date to "date" + 1, it will automatically affect months or even years if needed.

Also the Date object is mutable, which means doing this is enough:

dat = new Date(end_date.toDateString());
dat.setDate(dat.getDate() + 1);
axelduch
  • 10,769
  • 2
  • 31
  • 50
0

You can add a day by adding 86,400,000ms (1 day) to the current date.

function addDays(date, numOfDays) {
  date.setTime(date.getTime() + (86400000 * numOfDays));
}

You can support other date intervals like so:

function dateAdd(date, interval, units) {
  var d = new Date(date);
  switch (interval.toUpperCase()) {
    case 'YEAR'   :  d.setFullYear ( d.getFullYear() + units           ); break;
    case 'QUARTER':  d.setMonth    ( d.getMonth()    + units * 3       ); break;
    case 'MONTH'  :  d.setMonth    ( d.getMonth()    + units           ); break;
    case 'WEEK'   :  d.setDate     ( d.getDate()     + units * 7       ); break;
    case 'DAY'    :  d.setDate     ( d.getDate()     + units           ); break;
    case 'HOUR'   :  d.setTime     ( d.getTime()     + units * 3600000 ); break;
    case 'MINUTE' :  d.setTime     ( d.getTime()     + units * 60000   ); break;
    case 'SECOND' :  d.setTime     ( d.getTime()     + units * 1000    ); break;
    default       :  d             = undefined                          ; break;
  }
  return d;
}

function parseDate(dateString, delimiter) {
  var parts = dateString.split(delimiter).map(function(v) {
    return parseInt(v, 10);
  });
  return new Date(parts[2], parts[1], parts[0]);
}

function formatDate(date, delimiter) {
  return [ date.getDate(), date.getMonth(), date.getFullYear() ].map(function(v) {
    return v < 10 ? ('0' + v) : v;
  }).join(delimiter);
}

function get_renew_date() {
  var chkautorenew = document.getElementById("MainContent_chk_Isauto_Renew");
  if (chkautorenew.checked) {
    end_date = document.getElementById("MainContent_txtContract_End_date").value;
    renew_date = formatDate(dateAdd(parseDate(end_date, '/'), 'day', 1), '/');
    document.getElementById("MainContent_txtContract_Renew").value = renew_date;
  }
}

get_renew_date();
<input type="checkbox" id="MainContent_chk_Isauto_Renew" checked />

<input type="text" id="MainContent_txtContract_End_date" value="31/03/2016" />
<input type="text" id="MainContent_txtContract_Renew" value="" />
Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132