[JQuery] calculate the age

Thinking

  1. Create a Date object system time

  2. The target date for the conversion birthday

  3. Year subtraction age of acquisition

  4. If the result is less than 0 Results month, date or subtraction is less than zero, subtracting 1 Age 

  5. Return Age

 

Code

getAge function (dateString) { 
    // create a system date 
    var = new new Today a Date (); 
    // convert birth date to a date 
    var birthDate = new Date (dateString) ; 
    // After subtraction year are acquired 
    var age = today. the getFullYear () - birthDate.getFullYear (); 
    // month after obtaining the subtraction 
    var today.getMonth m = () - birthDate.getMonth (); 
    // a result if the result is less than 0 month, date or subtraction is less than 0, Age subtracted. 1 
    IF (m <0 || (m === 0 && today.getDate () <birthDate.getDate ())) { 
        age--; 
    } 
    // return result calculation is complete 
    return Age; 
}

  

Guess you like

Origin www.cnblogs.com/SoYang/p/11204235.html