user1910290
user1910290

Reputation: 537

Convert html5 date input to date object

I have a form setup with html5 date input what I wanna do is get that date add some days to it and then show the final result in another html5 date input

In my code here, the days are added to todays date.

how do I alter the Javascript so that the days are added to the user selected date.

current JS i am using:

var terms = $("#terms").val();
        var date = new Date();
        date.setDate(date.getDate() + terms);
        var day = ("0" + date.getDate()).slice(-2);
        var month = ("0" + (date.getMonth() + 1)).slice(-2);
        var final = date.getFullYear()+"-"+(month)+"-"+(day);
        $("#duedate").val(final);

Upvotes: 0

Views: 1518

Answers (1)

Ashley Medway
Ashley Medway

Reputation: 7301

You need to parse your terms val as an integer - parseInt(terms). Fiddle.

$('#terms').on('blur', function() {
    var terms = $("#terms").val();
    var date = new Date($("#date").val());
    date.setDate(date.getDate() + parseInt(terms));
    var day = ("0" + date.getDate()).slice(-2);
    var month = ("0" + (date.getMonth() + 1)).slice(-2);
    var final = date.getFullYear()+"-"+(month)+"-"+(day);
    $("#duedate").val(final);
});

Upvotes: 1

Related Questions