0

I am using date picker for the birth date selection. and I have three input box. after enter 31/08/1991 birthdate auto calculate year month and days like 21 year 1 months 2 Days.

$(function() {
        $("#dob").datepicker({
            changeMonth : true,
            changeYear : true,
            defaultDate: '-30yr',
            yearRange: 'c-25:c+35',

            onSelect: function(selectedDate) {
                var a = $("#dob").datepicker( "getDate" );

            var today = new Date();
                var age = Math.floor((today-a) / ( 24 * 60 * 60 * 1000 ));
                var year = Math.round(age/365);
                var month = Math.round(age/30);

                $('#ages').val(year); 


            } 
        });
        $("#dob").datepicker("option", "dateFormat", 'dd-mm-yy');

        $("input:submit, a, button", ".registerSubmit").button();



    });
4b0
  • 21,981
  • 30
  • 95
  • 142
Ashish Mehta
  • 7,226
  • 4
  • 25
  • 52

1 Answers1

0

You can use onselect event to get dates.

In your case, $(#date2) represents the next birthday

$('#date1').datepicker({
   onSelect: function(dateText, inst) { 
       var d1=new Date(dateText);
       // get date from other text field
       var d2=new Date($('#date2').val());
       // d2 -d1 gives result in milliseconds
       // calculate number of days by Math.abs((d2-d1)/86400000, as 24*3600*1000 = 86400000
       // and populate it to some text field #textfield
       $('#textfield').val((Math.abs((d2-d1)/86400000)));
   }
}); 

related topic (idea from Joakim Johansson): jQuery UI Datepicker difference in days

Community
  • 1
  • 1
sdespont
  • 13,915
  • 9
  • 56
  • 97