PEENGEE
PEENGEE

Reputation: 25

Javascript setDate doesnt accept input

    <code>
    var d2 =  $('#interval').val();
    var new_date = new Date(get_start_date); 
    new_date.setDate(new_date.getDate() + d2);
    var dd = new_date.getDate();
    var mm = new_date.getMonth() + 1;
    var y = new_date.getFullYear();
    var endDate = y + '-' + mm + '-' + dd;
    </code>

assuming d2 = 5

when im adding 5 dates to my current date, its not returning exact answer instead its adding months it becomes 2017-09-09 but when i just do this new_date.setDate(new_date.getDate() + 5) it gives me the correct output.

Upvotes: 0

Views: 43

Answers (2)

Gaurav Chaudhary
Gaurav Chaudhary

Reputation: 1501

Just parse you d2 as an integer

var d2 = parseInt($('#interval').val(),10);

Upvotes: 1

Vanessa
Vanessa

Reputation: 1

u can only use this arguments new Date() new Date(milliseconds) new Date(dateString) new Date(year, month, day, hours, minutes, seconds, milliseconds)

get_start_date is invailid ;)

var new_date = new Date(get_start_date);

Upvotes: 0

Related Questions