20 lines
512 B
TypeScript
20 lines
512 B
TypeScript
|
|
export function calculateAge(start: Date, end = new Date()) {
|
||
|
|
if (start.getTime() > end.getTime()) return;
|
||
|
|
|
||
|
|
let year = end.getFullYear() - start.getFullYear();
|
||
|
|
let month = end.getMonth() - start.getMonth();
|
||
|
|
let day = end.getDate() - start.getDate();
|
||
|
|
|
||
|
|
if (month < 0) year -= 1;
|
||
|
|
if (month < 0) month += 12;
|
||
|
|
if (day < 0) {
|
||
|
|
month -= 1;
|
||
|
|
day =
|
||
|
|
new Date(start.getFullYear(), start.getMonth(), 0).getDate() -
|
||
|
|
start.getDate() +
|
||
|
|
end.getDate();
|
||
|
|
}
|
||
|
|
|
||
|
|
return { year, month, day };
|
||
|
|
}
|