refactor: calculateAge Function
This commit is contained in:
@@ -52,7 +52,7 @@ export class HomeComponent implements OnInit, AfterViewInit, OnDestroy {
|
|||||||
};
|
};
|
||||||
|
|
||||||
protected readonly global: Global = global;
|
protected readonly global: Global = global;
|
||||||
protected readonly age: number | string = this.calculateAge(global.birthdate);
|
protected readonly age: number = this.calculateAge(global.birthdate);
|
||||||
|
|
||||||
protected isLongBioShown: boolean = false;
|
protected isLongBioShown: boolean = false;
|
||||||
protected isLongBioMounted: boolean = false;
|
protected isLongBioMounted: boolean = false;
|
||||||
@@ -462,12 +462,18 @@ export class HomeComponent implements OnInit, AfterViewInit, OnDestroy {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
private calculateAge(birthDateString: string): number | string {
|
private calculateAge(birthDateString: string): number {
|
||||||
let birthDate: Date = new Date(birthDateString);
|
const birthDate: Date = new Date(birthDateString);
|
||||||
let now: Date = new Date();
|
const now: Date = new Date();
|
||||||
let diffMs: number = now.getTime() - birthDate.getTime();
|
let age: number = now.getFullYear() - birthDate.getFullYear();
|
||||||
let diffYears: number = diffMs / (1000 * 60 * 60 * 24 * 365.25);
|
const hasBirthdayPassedThisYear: boolean =
|
||||||
let tempAge: string = diffYears.toFixed(1);
|
now.getMonth() > birthDate.getMonth() ||
|
||||||
return tempAge.endsWith('.0') ? Math.round(diffYears) : tempAge;
|
(now.getMonth() === birthDate.getMonth() && now.getDate() >= birthDate.getDate());
|
||||||
|
|
||||||
|
if (!hasBirthdayPassedThisYear) {
|
||||||
|
age -= 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return age;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user