- 이 함수는 birthday 라는 인자를 받습니다.
- 이 birthday 는 Date 객체 입니다. birthday 라는 인자를 넣었을 때, 현재를 기준으로 만으로 계산한 나이를 리턴 해주세요.
풀이.
- 현재시간 구하기
new Date()
- birthday의 year과 현재의 year 차이 구하기
getFullYear()
- 만 나이가 나오는 조건문
: return year 갭 -1
- 조건 1 : 현재와 birthday의 '월'이 같다 + 현재의 '일'이 birthday의 '일'보다 작다
- 조건2 : 현재의 '월'이 birthday의 '월' 보다 작다.
- 위의 조건 이외의 경우 (월&일 이 생일보다 큰 경우)
: return year 갭
코드:
function getWesternAge(birthday) {
let now = new Date();
let yeargap = now.getFullYear() - birthday.getFullYear();
if((now.getMonth()+1< birthday.getMonth()+1 ) || (now.getMonth()+1 === birthday.getMonth()+1 && now.getDate() < birthday.getDate())){
return yeargap -1
}
return yeargap
}