Last active
July 29, 2019 09:50
-
-
Save qkreltms/8946ee73f77aa86bd21ab2e1fd758483 to your computer and use it in GitHub Desktop.
만 나이 계산기
This file contains hidden or 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
| // 주어진 생일값과 현재 날짜값에 따라서 만 나이를 구한다. | |
| const manNai = (birthday: Date) => { | |
| const birthYear = birthday.getFullYear(); | |
| const birthMonth = birthday.getMonth() + 1; | |
| const birthDate = birthday.getDate(); | |
| const now = new Date(); | |
| const nowYear = now.getFullYear(); | |
| const nowMonth = now.getMonth() + 1; | |
| const nowDate = now.getDate(); | |
| let age: number | string = nowYear - birthYear; | |
| // 당해에 태어났을 때 | |
| if (age <= 0) { | |
| age = nowMonth - birthMonth; | |
| // 미래값 들어올 때 | |
| if (age < 0) { | |
| age = 0; | |
| } | |
| age = age + " 개월"; | |
| // 태어난 년도는 다르지만 1년도 안됐을 때 | |
| } else if (age === 1) { | |
| // 몇 개월 더 빨리 태워났을 때 | |
| if (birthMonth > nowMonth) { | |
| age = 12 - (birthMonth - nowMonth); | |
| age = age + " 개월"; | |
| // 11개월 12개월 사이일 때 | |
| } else if (birthMonth === nowMonth) { | |
| if (birthDate > nowDate) { | |
| age = 11 + " 개월"; | |
| } | |
| } | |
| // 태어난지 1년 넘었을 때 | |
| } else { | |
| if (birthMonth > nowMonth) { | |
| age--; | |
| } else if (birthMonth === nowMonth) { | |
| // 태어난 날이 현재 보다 적으면 아직 생일 전이므로 나이를 먹지 않는다. | |
| if (birthDate > nowDate) { | |
| age--; | |
| } | |
| } | |
| // 미래에 태어났을 경우 | |
| if (age < 0) { | |
| age = 0 | |
| } | |
| return console.log(age); | |
| }; | |
| // 태어난 날 현재 결과 값 | |
| // 2019-07-18 2019-07-18 0 개월 | |
| // 2019-06-18 2019-07-18 1 개월 | |
| //... | |
| // 2018-12-18 2019-07-18 7 개월 | |
| // 2018-07-18 2019-07-18 1 //1 살 | |
| // 2018-07-19 2019-07-18 11 개월 | |
| // 1995-01-15 2019-07-18 24 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment