JQuery Age calculation on date

前端 未结 7 1311
挽巷
挽巷 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条回答
  •  猫巷女王i
    2020-12-15 07:16

    That's what worked for me while all the rest gave NaN:

    That only works if the dob format is mm/dd/yy

    function getAge(dob) { return ~~((new Date()-new Date(dob))/(31556952000)) }
    $("#dob").val()
    $(dob).change(function(){
      $('.age').val(getAge($(this).val()));
    });
    

    Please see my jsfiddle http://jsfiddle.net/A888/98c21nbz/1/

提交回复
热议问题