Add utility function (calculate age in range)
This commit is contained in:
parent
fa1f8dc3fb
commit
747d8c6b1c
1 changed files with 19 additions and 0 deletions
19
src/interfaces/utils.ts
Normal file
19
src/interfaces/utils.ts
Normal file
|
|
@ -0,0 +1,19 @@
|
||||||
|
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 };
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue