Age from Date of Birth using JQuery

后端 未结 7 1654
轮回少年
轮回少年 2020-11-30 06:42

I need to calculate if someone is over 18 from their date of birth using JQuery.

var curr = new Date();
curr.setFullYear(curr.getFullYear() - 18);

var dob =         


        
7条回答
  •  無奈伤痛
    2020-11-30 07:08

    keep in mind that all above answers work only for date using separator '/'. if you are using other that this then you have to replace that separator first.

    var startDate = $('#start_date').val().replace('-','/');
    var endDate = $('#end_date').val().replace('-','/');
    
    if(startDate > endDate){
       // do stuff here...
    }
    

    happy coding :D

提交回复
热议问题