2

I have HTML code as below:

<script type="text/javascript">
$(document).ready(function(){
    $("#useDatePicker").mouseout(function{
        //$("#result").text() = $("#useDatePicker").text();
        alert(1);
    });
});

</script>
</head>

<body>
<form method="post" name="frm" id="myForm">
<input type="text" name="date" id="useDatePicker"/>
    <input type="text" name="result" id="result" />
</form>

What I need:

when the user select date from date Picker, it will subtract with date now automatic and the result will be in <input type="text" name="result" id="result" />

Problem

I have no concept with it.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Pov Nu
  • 68
  • 1
  • 8

1 Answers1

0

may be you can use onselect option of date picker of jquery ui

demo: fiddle

 $('#useDatePicker').datepicker({
onSelect: function (date) {

    var today = new Date();

    var result = DateDiff(today, new Date(date));

    $('#result').val(result);

}

});

function DateDiff(date1, date2) {
var diff = date1 - date2;
var num_years = diff / 31536000000;
var num_months = (diff % 31536000000) / 2628000000;
var num_days = ((diff % 31536000000) % 2628000000) / 86400000;

 return Math.floor(num_years) + "years " + Math.floor(num_months) + "months " +    Math.floor(num_days) + "days";
}
fuzionpro
  • 601
  • 6
  • 19
  • Many thanks for your answer,but the result I want to get as `year month day` for example:`2year 3months 10days` after subtraction.Please help me to fix this.Thanks. – Pov Nu Oct 23 '12 at 07:40
  • @PovNu i have updated the fiddle i think it achieves your result – fuzionpro Oct 23 '12 at 09:16