fix(main): age calculation
This commit is contained in:
parent
3249c0f970
commit
9e58978b4d
1 changed files with 74 additions and 27 deletions
|
@ -1,32 +1,79 @@
|
||||||
// my birthday :D
|
// my birthday in KST :D
|
||||||
const birthYear = 2002
|
const birth = new Date("2002-07-30, 00:00:00.000 +09:00")
|
||||||
const birthMonth = 7
|
|
||||||
const birthDate = 30
|
const dateFormatOption: Intl.DateTimeFormatOptions = {
|
||||||
|
timeZone: "Asia/Seoul",
|
||||||
|
timeZoneName: "longOffset",
|
||||||
|
hourCycle: "h23",
|
||||||
|
year: "numeric",
|
||||||
|
month: "numeric",
|
||||||
|
day: "numeric",
|
||||||
|
hour: "numeric",
|
||||||
|
minute: "numeric",
|
||||||
|
second: "numeric",
|
||||||
|
fractionalSecondDigits: 3,
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets developomp's age with decimal precision
|
* Gets developomp's age with decimal precision
|
||||||
|
*
|
||||||
|
* @param now - current `Date` in KST
|
||||||
*/
|
*/
|
||||||
export default function getAge(): number {
|
export default function getAge(
|
||||||
const now = Date.now()
|
now: Date = new Date(new Date().toLocaleString("en-US", dateFormatOption))
|
||||||
const date = new Date()
|
): number {
|
||||||
const year = date.getFullYear()
|
return ageInt(now) + ageDecimal(now)
|
||||||
|
}
|
||||||
// integer calculation
|
|
||||||
|
/**
|
||||||
const isOverBirthDay =
|
* Calculates the integer component of the age
|
||||||
birthMonth > date.getMonth() + 1 ||
|
*/
|
||||||
(birthMonth === date.getMonth() + 1 && birthDate >= date.getDate())
|
function ageInt(now: Date): number {
|
||||||
const ageInt = year - birthYear - (isOverBirthDay ? 1 : 0)
|
return (
|
||||||
|
now.getFullYear() - birth.getFullYear() - (isOverBirthDay(now) ? 0 : 1)
|
||||||
// decimal calculation
|
)
|
||||||
|
}
|
||||||
const msThisYear = Date.UTC(year, 0, 0)
|
|
||||||
const msThisBD = Date.UTC(year, birthMonth - 1, birthDate)
|
/**
|
||||||
// number of milliseconds since the beginning of this year
|
* Calculates the decimal component of the age
|
||||||
const msSinceThisYear = now - msThisYear
|
*/
|
||||||
// number of milliseconds from the beginning of this year to my birthday
|
function ageDecimal(now: Date): number {
|
||||||
const msToBD = msThisBD - msThisYear
|
// millisecond timestamp of my last birthday
|
||||||
const ageDecimal = msSinceThisYear / msToBD
|
const BDPrev = new Date(birth).setFullYear(
|
||||||
|
now.getFullYear() - (isOverBirthDay(now) ? 0 : 1)
|
||||||
return ageInt + ageDecimal
|
)
|
||||||
|
|
||||||
|
// millisecond timestamp of my upcoming birthday
|
||||||
|
const BDNext = new Date(birth).setFullYear(
|
||||||
|
now.getFullYear() + (isOverBirthDay(now) ? 1 : 0)
|
||||||
|
)
|
||||||
|
|
||||||
|
// milliseconds since my last birthday
|
||||||
|
const msSinceLastBD = now.getTime() - BDPrev
|
||||||
|
|
||||||
|
return msSinceLastBD / (BDNext - BDPrev)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* tests if today is at or after this year's birthday
|
||||||
|
*
|
||||||
|
* ...| Dec 31 | Jan 1 | ... | July 29 | Jul 30 | July 31 | ... | Dec 31 | Jan 1 | ...
|
||||||
|
* ...| true | false | ... | false | true | true | ... | true | false | ...
|
||||||
|
*/
|
||||||
|
function isOverBirthDay(now: Date): boolean {
|
||||||
|
if (birth.getMonth() < now.getMonth()) return true
|
||||||
|
|
||||||
|
if (birth.getMonth() === now.getMonth() && birth.getDate() <= now.getDate())
|
||||||
|
return true
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
export const testing = {
|
||||||
|
birth,
|
||||||
|
dateFormatOption,
|
||||||
|
getAge,
|
||||||
|
ageInt,
|
||||||
|
ageDecimal,
|
||||||
|
isOverBirthDay,
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue