1
function addDays(){ 
      var myDate =document.getElementById('treatdate'); 
      var numberOfDaysToAdd = document.getElementById('resultdays'); 
      var tset = numberOfDaysToAdd.value; 
      var result1 = myDate.value.addMonths(parseInt(tset)); 
      var pldate= moment(result1).format('YYYY-MM-DD');
      return pldate; }

'treatdate' is an id for a treatment date which is pulled from my database. Thanks in advance.

Manoj
  • 67
  • 6

3 Answers3

0

If your date format is yyyy-MM-dd

var myDate = new Date(document.getElementById('treatdate').value); 

this will a date and time.
Example:

var dd = new Date("2014-02-02 11:11:11")

console log

Sun Feb 02 2014 11:11:11 GMT+0000 (GMT Standard Time)
Matas Vaitkevicius
  • 58,075
  • 31
  • 238
  • 265
0

See if this snippet can help you understand how to manipulate dates. To change/add the months to a Date() object you can use the setMonth() method.

numberOfMonthsToAdd = 5; // the number of months that you want to add
date = new Date(); // creating a Date object
date.setMonth(numberOfMonthsToAdd); // adding the number of months

Note that you may add numbers beyond 12 - the object handles the date, and jumps to the next year.

lfarroco
  • 574
  • 6
  • 14
0

You can always use moment.js library for Date manipulations. http://momentjs.com/

For your problem you can do the following.

function addDays(){
        var datefrmDb = $('#treatdate').val();
        var monthstoadd= $('#resultdays').val();
        var new_date = (moment(datefrmDb, "YYYY-MM-DD").add('months', monthstoadd)).format("YYYY-MM-DD");
        return new_date;
    }