diff --git a/src/interfaces/utils.ts b/src/interfaces/utils.ts new file mode 100644 index 00000000..aaf4265e --- /dev/null +++ b/src/interfaces/utils.ts @@ -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 }; +}