24 lines
875 B
TypeScript
24 lines
875 B
TypeScript
|
|
/**
|
||
|
|
* คำนวณอายุงานจากจำนวนวันรวม
|
||
|
|
* @param totalDays จำนวนวันรวม
|
||
|
|
* @returns { year, month, day } ปี เดือน วัน
|
||
|
|
*/
|
||
|
|
export function calculateTenure(totalDays: number) {
|
||
|
|
// 1. แปลงเป็น year เต็ม
|
||
|
|
const year = Math.floor(totalDays / 365.2524);
|
||
|
|
|
||
|
|
// 2. วันที่เหลือหลังหัก year ออก
|
||
|
|
const remainAfterYear = totalDays - year * 365.2524;
|
||
|
|
|
||
|
|
// 3. แปลงเป็น month เต็ม
|
||
|
|
const month = Math.floor(remainAfterYear / 30.4375);
|
||
|
|
|
||
|
|
// 4. วันที่เหลือหลังหัก month ออก
|
||
|
|
const remainAfterMonth = remainAfterYear - month * 30.4375;
|
||
|
|
|
||
|
|
// 5. ปัดลง เฉพาะวัน
|
||
|
|
const day = Math.floor(remainAfterMonth);
|
||
|
|
|
||
|
|
return { year, month, day };
|
||
|
|
}
|