0

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);
clstrfsck
  • 14,715
  • 4
  • 44
  • 59
user1910290
  • 537
  • 4
  • 15
  • 28
  • What is 15, 30, 60?! Days? – Ashley Medway Feb 09 '14 at 08:01
  • @AshleyMedway Those are the days which I want to add to the date which the user selects. – user1910290 Feb 09 '14 at 08:02
  • similar : http://stackoverflow.com/questions/3818193/how-to-add-number-of-days-to-todays-date – Istiaque Ahmed Feb 09 '14 at 08:05
  • @IstiaqueAhmed that question answers only adding days, what I wanna do is that i want to pick date from one input add some days to it and show the addition to another date html5 input – user1910290 Feb 09 '14 at 08:09
  • Hi @user1910290 - you seem new here. Can you please put the whole code into your question? That JSFiddle won't stick around forever, but StackOverflow questions do... so someday your question will point at a dead link, which won't let anybody learn from what you have asked. I notice that the code in the JSFiddle is not exactly like what you have posted above, which is why I'm asking... – Taryn East Feb 09 '14 at 08:20

1 Answers1

1

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);
});
Ashley Medway
  • 7,151
  • 7
  • 49
  • 71
  • sorry for the wrong fiddle link heres the updated one : http://jsfiddle.net/CjEEs/4/ what I wanna do is that I want the days to be added to the datepicker above the terms selectbox and the final result should be shown to the datepicker below the tersm selectbox – user1910290 Feb 09 '14 at 08:07