hrms-api-org/src/utils/tenure.ts

24 lines
875 B
TypeScript
Raw Normal View History

/**
*
* @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 };
}