Question

I managed to get this to run but only up-to a point. What I'm trying to do is the following:

There are 2 datepickers and one text field.

  1. Once date1 is selected, will then auto-generate date2 with a "future date" say... 6, 12, 24, 36 month.

  2. Based on the info in those two date fields, I need to calculate the remaining days between date1 and date2.

e.g. DATE1 (06/01/2012) -> DATE2 Gets Date from DATE1 and ADDS 12 OR 24. (06/01/2013) -> DAYS (365)

Below is the code in case someone can help out. Thanks!

$(function() {
$("#dop").datepicker
    ({
    dateFormat: "dd/mm/yy",
    onSelect: function(dateText, instance) {
        date = $.datepicker.parseDate(instance.settings.dateFormat, dateText, instance.settings);
        date.setMonth(date.getMonth() + 12);
        $("#wed").datepicker("setDate", date);
        if ($('#dop') && $('#wed')) {
        $("#days").val($("#dop").getTime() - $("#wed").getTime() / 86400000);
        }
    }
});
$("#wed").datepicker({
    dateFormat: "dd/mm/yy",  
});

});

http://jsfiddle.net/TCXcX/332/

Was it helpful?

Solution

Only got so far before must stop (time). Here's where I got to, perhaps it will help.

You must turn your field values back into dates, as so:

            var t1 = $("#dop").val();
            var t1d = new Date(t1);

Fully working jsFiddle here


Finished example. If this doesn't do what you want, please respond with details:

$(function() {
    $("#dop").datepicker({
        dateFormat: "dd/mm/yy",
        onSelect: function(dateText, instance) {
            date = $.datepicker.parseDate(instance.settings.dateFormat, dateText, instance.settings);
            date.setMonth(date.getMonth() + 12);
            $("#wed").datepicker("setDate", date);
            if ($('#dop') && $('#wed')) {
                var t1 = $("#dop").val();
                //alert(t1);
                var t1d = new Date(t1);
                //alert(t1d);
                var t2 = $("#wed").val();
                var t2d = new Date(t2);
                var diff = (t2d - t1d)/86400000;
                //alert(diff);
                $('#days').val(diff);
            }
        }
    });
    $("#wed").datepicker({
        dateFormat: "dd/mm/yy",  
    });

});

Here's another jsFiddle example where the number of months to add is stored in a hidden input field.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top