2011-10-20 13 views
5
function getAge(dateString) { 
    var today = new Date(); 
    var birthDate = new Date(dateString); 
    var age = today.getFullYear() - birthDate.getFullYear(); 
    var m = today.getMonth() - birthDate.getMonth(); 
    if (m < 0 || (m === 0 && today.getDate() < birthDate.getDate())) { 
     age--; 
    } 
    document.write(age); 

} 

getAge("01/20/2011") 

Questo mi mostra 0 anni, ma vorrei mostrare 10 Months e 9 mesi fino a quando il suo compleanno arriva 10/20/2011.Calcolo età in mesi e giorni

+0

Che cosa esattamente vuoi ottenere. Mesi rimasti fino al prossimo compleanno? o l'età della persona? Si prega di precisare. Grazie. – HashimR

risposta

10

Capita spesso che si desidera conoscere l'età di una persona sarebbe in una data specifica, o gli anni , mesi e giorni tra due date.

Se si desidera l'età attuale, passare un singolo argomento data o datestring.

function getAge(fromdate, todate){ 
    if(todate) todate= new Date(todate); 
    else todate= new Date(); 

    var age= [], fromdate= new Date(fromdate), 
    y= [todate.getFullYear(), fromdate.getFullYear()], 
    ydiff= y[0]-y[1], 
    m= [todate.getMonth(), fromdate.getMonth()], 
    mdiff= m[0]-m[1], 
    d= [todate.getDate(), fromdate.getDate()], 
    ddiff= d[0]-d[1]; 

    if(mdiff < 0 || (mdiff=== 0 && ddiff<0))--ydiff; 
    if(mdiff<0) mdiff+= 12; 
    if(ddiff<0){ 
     fromdate.setMonth(m[1]+1, 0); 
     ddiff= fromdate.getDate()-d[1]+d[0]; 
     --mdiff; 
    } 
    if(ydiff> 0) age.push(ydiff+ ' year'+(ydiff> 1? 's ':' ')); 
    if(mdiff> 0) age.push(mdiff+ ' month'+(mdiff> 1? 's':'')); 
    if(ddiff> 0) age.push(ddiff+ ' day'+(ddiff> 1? 's':'')); 
    if(age.length>1) age.splice(age.length-1,0,' and ');  
    return age.join(''); 
} 


getAge("1/25/1974")>> 37 years 8 months and 26 days 

getAge("9/15/1984")>> 27 years 1 month and 5 days 

getAge("12/20/1984","10,20,2011")>>26 years and 9 months 

getAge(new Date(),"12/25/2011")+' till Christmas'>> 
2 months and 5 days till Christmas 

getAge("6/25/2011")>> 3 months and 25 days 
+0

Penso che ci dovrebbe essere se (mdiff <0) mdiff + = 12; invece di if (mdiff <0) mdiff + = 11; Altrimenti salta un mese quando l'età è inferiore a un anno http://jsfiddle.net/nxtwrld/nmp7F/ – nxtwrld

+0

C'è un bug nella soluzione. Diciamo che la data corrente è il 16/02/2016 e si passa la data di nascita come il 17/02/2015. Quindi il codice sopra riportato restituisce l'età di 27 giorni. –

4

Con questo si può avere un testo età descrittiva: giorni, mesi e anni:

function getAge(dateString) { 

    var birthdate = new Date(dateString).getTime(); 
    var now = new Date().getTime(); 
    // now find the difference between now and the birthdate 
    var n = (now - birthdate)/1000; 

    if (n < 604800) { // less than a week 
    var day_n = Math.floor(n/86400); 
    return day_n + ' day' + (day_n > 1 ? 's' : ''); 
    } else if (n < 2629743) { // less than a month 
    var week_n = Math.floor(n/604800); 
    return week_n + ' week' + (week_n > 1 ? 's' : ''); 
    } else if (n < 63113852) { // less than 24 months 
    var month_n = Math.floor(n/2629743); 
    return month_n + ' month' + (month_n > 1 ? 's' : ''); 
    } else { 
    var year_n = Math.floor(n/31556926); 
    return year_n + ' year' + (year_n > 1 ? 's' : ''); 
    } 
} 


var age = getAge("01/20/2011"); 
alert(age); 
+0

questa risposta mi ha aiutato moltissimo :) – Madhu

Problemi correlati