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

69 lines
1.4 KiB
TypeScript

export function calculateAge(start: Date, end = new Date()) {
if (start.getTime() > end.getTime()) return null;
let year = end.getFullYear() - start.getFullYear();
let month = end.getMonth() - start.getMonth();
let day = end.getDate() - start.getDate();
if (month < 0) {
month += 12;
year -= 1;
}
if (day < 0) {
month -= 1;
day =
new Date(start.getFullYear(), start.getMonth(), 0).getDate() -
start.getDate() +
end.getDate();
}
return { year, month, day };
}
export function calculateRetireDate(birthDate: Date) {
let dd = birthDate.getDate();
let mm = birthDate.getMonth();
let yy = birthDate.getFullYear();
let flag = true;
switch (mm) {
case 10:
if (dd >= 2) flag = false;
break;
case 11:
break;
case 12:
break;
}
if (flag) return new Date(`${yy + 60}-09-30T00:00:00.000+07:00`);
return new Date(`${yy + 61}-09-30T00:00:00.000+07:00`);
}
export function calculateRetireLaw(birthDate: Date) {
let yy = birthDate.getFullYear();
return new Date(`${yy + 60}-09-30T00:00:00.000+07:00`);
}
export function calculateRetireYear(birthDate: Date) {
let dd = birthDate.getDate();
let mm = birthDate.getMonth();
let yy = birthDate.getFullYear();
let flag = true;
switch (mm) {
case 10:
if (dd >= 2) flag = false;
break;
case 11:
break;
case 12:
break;
}
if (flag) return yy + 60;
return yy + 61;
}