locked
How to Calculate Age from DatePicker RRS feed

  • Question

  • User-712926555 posted

    Hi,

    If remove the date Format, It calculate age Perfectly

    
    
       $("#<%=txtdob.ClientID %>").datepicker({
                    onSelect: function (value, ui) {
                        var today = new Date(),
                dob = new Date(value),
                age = new Date(today - dob).getFullYear() - 1970;
                $("#<%=txtage.ClientID %>").val(age);
                    },
                    maxDate: '+0d',
                    yearRange: '1960:2010',
                    changeMonth: true,
                    changeYear: true,
                    dateFormat: 'dd-mm-yy'
                });

    If I  Change Date format as dd-mm-yy it shows NaN as age.

    Monday, September 19, 2016 12:58 PM

Answers

  • User-654786183 posted

    Hisanth,

    You have to parse the date to the same format as "today".

    Replace the line "dob = new Date(value)" with

    var selectedDate = value.split("-");
    var dob = new Date(selectedDate[2], selectedDate[1] - 1, selectedDate[0]);
    

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, September 19, 2016 1:32 PM