Last active
April 6, 2017 00:53
-
-
Save rhostem/f7a722d3657bfd92c5abdffd4cafd117 to your computer and use it in GitHub Desktop.
calcAge.js - 만 나이 계산
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 생년월일을 기반으로 현재 시점의 만 나이를 계산한다. | |
* @param {[string]} birthDate [YYYY-MM-DD 형식] | |
* @param {[type]} calcPointDate [날짜 계산 기준일] | |
* @return {[number]} [만 나이] | |
*/ | |
export const calcAge = (birthDate, calcPointDate) => { | |
if (!isValidDateString(birthDate)) { | |
return 0; | |
} | |
// 입력한 생일이 현재보다 미래일 경우 | |
if (new Date() < new Date(birthDate)) { | |
return 0; | |
} | |
if (calcPointDate && !isValidDateString(calcPointDate)) { | |
return 0; | |
} | |
// 나이 계산 시점. 기본으로 현재 | |
const calcPointTime = calcPointDate ? new Date(calcPointDate) : new Date(); | |
const birthDateTime = new Date(birthDate); | |
// 태어난 날짜의 년월일 | |
const birth = { | |
year: birthDateTime.getFullYear(), | |
month: birthDateTime.getMonth() + 1, | |
day: birthDateTime.getDate(), | |
}; | |
// 현재 날짜의 년월일 | |
const current = { | |
year: calcPointTime.getFullYear(), | |
month: calcPointTime.getMonth() + 1, | |
day: calcPointTime.getDate(), | |
}; | |
const timeOfBirthDay = new Date(`${birth.month}-${birth.day}`); | |
const timeOfCurrent = new Date(`${current.month}-${current.day}`); | |
// 생일 이전인지 | |
const isBeforeBirthDay = timeOfCurrent < timeOfBirthDay; | |
// 만 나이 계산 | |
const age = isBeforeBirthDay ? current.year - birth.year - 1 : current.year - birth.year; | |
return age; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
describe('calcAge(만 나이 계산) 테스트 :', () => { | |
it('생일로 전달한 문자열이 적절한 데이트스트링이 아니다', () => { | |
expect(calcAge('2000-01-aa')).to.equal(0); | |
}); | |
it('기준시간으로 전달한 문자열이 적절한 데이트스트링이 아니다', () => { | |
expect(calcAge('2000-01-01', '2016-aa-aa')).to.equal(0); | |
}); | |
it('생일 이전인 경우', () => { | |
expect(calcAge('2000-07-01', '2016-01-19')).to.equal(15); | |
}); | |
it('생일 이후인 경우', () => { | |
expect(calcAge('2000-07-01', '2016-11-19')).to.equal(16); | |
}); | |
it('생일날인 경우', () => { | |
expect(calcAge('2000-07-01', '2016-07-01')).to.equal(16); | |
}); | |
it('첫 생일이 지나지 않은 경우', () => { | |
expect(calcAge('2000-07-01', '2000-08-01')).to.equal(0); | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 적절한 데이트스트링인지 검사한다. | |
* @param {[String]} dateString [ex. 2015-01-01] | |
*/ | |
export const isValidDateString = (dateString) => { | |
const date = new Date(dateString); | |
if (isNaN(date.getTime())) { | |
return false; | |
} | |
return true; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment