JQuery Age calculation on date

前端 未结 7 1300
挽巷
挽巷 2020-12-15 06:34

Am I missing something in the following jQuery code?

var dob = $(\'#date\').val();
if(dob != \'\'){
    var today = new Date();
    var dayDiff = Math.ceil(t         


        
7条回答
  •  既然无缘
    2020-12-15 07:28

    $('#date').val() returns the string '1988-04-07'. You need to parse it into an actual number.

    dob = new Date(dob);
    var today = new Date();
    var age = Math.floor((today-dob) / (365.25 * 24 * 60 * 60 * 1000));
    $('#age').html(age+' years old');
    

    As @esqew points out, you also need to change id="#date" to id="date".

提交回复
热议问题