fix:function_calculateAge
This commit is contained in:
parent
8867829211
commit
6f40ee330c
10 changed files with 41 additions and 152 deletions
|
|
@ -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} วัน`;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue