fix:function_calculateAge

This commit is contained in:
DESKTOP-1R2VSQH\Lenovo ThinkPad E490 2026-01-21 16:16:00 +07:00
parent 8867829211
commit 6f40ee330c
10 changed files with 41 additions and 152 deletions

View file

@ -1,3 +1,5 @@
import moment from "moment";
/**
*
*
@ -28,3 +30,33 @@ export function calculateFiscalYear(date: Date) {
const month = date.getMonth() + 1;
return month >= 10 ? date.getFullYear() + 1 : date.getFullYear();
}
/**
*
* @param birthDate
* @returns "อายุเกิน 60 ปี" null null
*/
export function calculateAge(birthDate: Date | null) {
if (!birthDate) return null;
// .startOf('day') จะเซ็ตเวลาเป็น 00:00:00.000 ของวันนั้นๆ
const birth = moment(birthDate).startOf("day");
const now = moment().startOf("day");
if (!birth.isValid()) return "Invalid Date";
// คราวนี้การลบกันจะสนแค่ "วันที่" เท่านั้น
const years = now.diff(birth, "years");
birth.add(years, "years");
const months = now.diff(birth, "months");
birth.add(months, "months");
const days = now.diff(birth, "days");
if (years >= 60) {
return "อายุเกิน 60 ปี";
}
return `${years} ปี ${months} เดือน ${days} วัน`;
}