2016-08-02 235 views
0

我一直在使用下面的代碼,但是當談到JS時,我是一個完整的初學者。我從ygssoni的代碼here中修改了這個,Angela編輯了一個更靈活的日期輸入。按月計算年齡,天

我該如何修改這個來報告某人在幾個月和幾天內的年齡,因爲他們的出生日期?

function getAge(dateString) { 
    var now = new Date(); 
    var today = new Date(now.getYear(),now.getMonth(),now.getDate()); 

    var yearNow = now.getYear(); 
    var monthNow = now.getMonth(); 
    var dateNow = now.getDate(); 

    var dob = new Date(dateString); 

    var yearDob = dob.getYear(); 
    var monthDob = dob.getMonth(); 
    var dateDob = dob.getDate(); 
    var age = {}; 
    var ageString = ""; 
    var yearString = ""; 
    var monthString = ""; 
    var dayString = ""; 


    yearAge = yearNow - yearDob; 

    if (monthNow >= monthDob) 
    var monthAge = monthNow - monthDob; 
    else { 
    yearAge--; 
    var monthAge = 12 + monthNow -monthDob; 
    } 

    if (dateNow >= dateDob) 
    var dateAge = dateNow - dateDob; 
    else { 
    monthAge--; 
    var dateAge = 31 + dateNow - dateDob; 

    if (monthAge < 0) { 
     monthAge = 11; 
     yearAge--; 
    } 
    } 

    age = { 
     years: yearAge, 
     months: monthAge, 
     days: dateAge 
     }; 

    if (age.years > 1) yearString = " years"; 
    else yearString = " year"; 
    if (age.months> 1) monthString = " months"; 
    else monthString = " month"; 
    if (age.days > 1) dayString = " days"; 
    else dayString = " day"; 


    if ((age.years > 0) && (age.months > 0) && (age.days > 0)) 
    ageString = age.years + yearString + ", " + age.months + monthString + ", and " + age.days + dayString + " old."; 
    else if ((age.years == 0) && (age.months == 0) && (age.days > 0)) 
    ageString = "Only " + age.days + dayString + " old!"; 
    else if ((age.years > 0) && (age.months == 0) && (age.days == 0)) 
    ageString = age.years + yearString + " old. Happy Birthday!!"; 
    else if ((age.years > 0) && (age.months > 0) && (age.days == 0)) 
    ageString = age.years + yearString + " and " + age.months + monthString + " old."; 
    else if ((age.years == 0) && (age.months > 0) && (age.days > 0)) 
    ageString = age.months + monthString + " and " + age.days + dayString + " old."; 
    else if ((age.years > 0) && (age.months == 0) && (age.days > 0)) 
    ageString = age.years + yearString + " and " + age.days + dayString + " old."; 
    else if ((age.years == 0) && (age.months > 0) && (age.days == 0)) 
    ageString = age.months + monthString + " old."; 
    else ageString = "Oops! Could not calculate age!"; 

    return ageString; 
} 


alert(getAge('09/09/1989')); 
+2

請試一下,然後問我們,爲什麼它不工作。 –

+0

您是否要求將年份換算爲幾個月? (即,而不是「2年,2個月,3周」它會給「26個月,3周」?) –

+0

@JonathanM就像我所說的,當談到JS時,我是一個完整的初學者。我發現這個代碼並且很幸運,它有點接近我正在尋找並且實際工作。 – 406LQE

回答

1

你應該去除基本上所有的「年」,在你的代碼的後面部分,並重新編寫大量的if/else部分這樣jsFiddle。我唯一改變的部分是線48 &下:

var totMonths = age.months + 12 * age.years; 
if (totMonths > 1) monthString = " months"; 
else monthString = " month"; 
if (age.days > 1) dayString = " days"; 
else dayString = " day"; 


if ((totMonths == 0) && (age.days > 0)) { 
    ageString = "Only " + age.days + dayString + " old!"; 
} else if ((totMonths > 0) && (age.days == 0)) { 
    ageString = totMonths + monthString + " old."; 
} else if ((totMonths > 0) && (age.days > 0)) 
    ageString = totMonths + monthString + " and " + age.days + dayString + " old."; 
} else ageString = "Oops! Could not calculate age!"; 
+0

感謝您的幫助,現在我至少有一個地方可以開始。 – 406LQE

0

試試這個:

var dob = new Date("1/1/2016"); 
    var now = new Date(); 
    var timeMiliseconds = Math.abs(now.getTime() - dob.getTime()); 
    var days = Math.ceil(timeMiliseconds/(1000 * 3600 * 24)); 
    var monthsElapsed = Math.ceil(timeMiliseconds/(1000 * 3600 * 24 * 30)) 
    var daysElapsed = days % 30 
    alert(monthsElapsed); 
    alert(daysElapsed);