2016-08-02 3 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

시도하십시오 무언가를 작동하지 않는 이유를 다음 우리에게 내가 변경 유일한 부분은 라인 (48) & 낮았다. –

+0

몇 달에서 몇 달로 변환 하시겠습니까? (즉, "2 Years, 2 Months, 3 Weeks"대신에 "26 Months, 3 Weeks"를 줄 것입니까?) –

+0

@JonathanM 내가 언급했듯이 JS에 관해서는 완전한 초보자입니다. 나는이 코드를 발견하고 그것이 내가 찾고 있었고 실제로 작동했던 것에 다소 가깝다는 것을 운이 좋게 얻었다. – 406LQE

답변

1

당신은 기본적으로 코드의 후반 부분에서 "년"에 대한 모든 참조를 제거하고 대형을 다시 작성해야이 jsFiddle 같은/다른 섹션 경우.

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); 
관련 문제