hrms-api-development/src/interfaces/extension.ts
2026-01-30 15:42:47 +07:00

176 lines
4.4 KiB
TypeScript

class Extension {
public static checkDateTime(value: any, format: "dd/MM/yyyy" | "yyyy-MM-dd"): Date | null {
if (value == null) return null;
let dateStr = String(value).trim();
if (!dateStr) return null;
const parts = dateStr.replace(/-/g, "/").split("/");
if (parts.length !== 3) return null;
let year = 0, month = 0, day = 0;
switch (format) {
case "dd/MM/yyyy":
year = Number(parts[2]);
month = Number(parts[1]);
day = Number(parts[0]);
break;
case "yyyy-MM-dd":
year = Number(parts[0]);
month = Number(parts[1]);
day = Number(parts[2]);
break;
}
if (isNaN(year) || isNaN(month) || isNaN(day)) return null;
// พ.ศ. → ค.ศ.
if (year > 2500) year -= 543;
// clamp month
if (month < 1 || month > 12) month = 1;
// clamp day
const maxDay = new Date(year, month, 0).getDate();
if (day < 1) day = 1;
else if (day > maxDay) day = maxDay;
const date = new Date(year, month - 1, day);
return isNaN(date.getTime()) ? null : date;
}
public static ToThaiMonth(value: number) {
switch (value) {
case 1:
return "มกราคม";
case 2:
return "กุมภาพันธ์";
case 3:
return "มีนาคม";
case 4:
return "เมษายน";
case 5:
return "พฤษภาคม";
case 6:
return "มิถุนายน";
case 7:
return "กรกฎาคม";
case 8:
return "สิงหาคม";
case 9:
return "กันยายน";
case 10:
return "ตุลาคม";
case 11:
return "พฤศจิกายน";
case 12:
return "ธันวาคม";
default:
return "";
}
}
public static ToThaiShortMonth(value: number) {
switch (value) {
case 1:
return "ม.ค.";
case 2:
return "ก.พ.";
case 3:
return "มี.ค.";
case 4:
return "เม.ย.";
case 5:
return "พ.ค.";
case 6:
return "มิ.ย.";
case 7:
return "ก.ค.";
case 8:
return "ส.ค.";
case 9:
return "ก.ย.";
case 10:
return "ต.ค.";
case 11:
return "พ.ย.";
case 12:
return "ธ.ค.";
default:
return "";
}
}
public static ToThaiYear(value: number) {
if (value < 2400) return value + 543;
else return value;
}
public static ToCeYear(value: number) {
if (value >= 2400) return value - 543;
else return value;
}
public static ToThaiNumber(value: string) {
let arabicNumbers = "0123456789";
let thaiNumbers = "๐๑๒๓๔๕๖๗๘๙";
let result = "";
for (let digit of value) {
let index = arabicNumbers.indexOf(digit);
if (index >= 0) {
result += thaiNumbers[index];
} else {
result += digit;
}
}
return result;
}
public static ToThaiFullDate(value: Date) {
let yy = value.getFullYear() < 2400 ? value.getFullYear() + 543 : value.getFullYear();
return (
"วันที่ " +
value.getDate() +
" เดือน " +
Extension.ToThaiMonth(value.getMonth() + 1) +
" พ.ศ. " +
yy
);
}
public static ToThaiFullDate2(value: Date) {
let yy = value.getFullYear() < 2400 ? value.getFullYear() + 543 : value.getFullYear();
return value.getDate() + " " + Extension.ToThaiMonth(value.getMonth() + 1) + " " + yy;
}
public static ToThaiFullDate3(value: Date) {
let yy = value.getFullYear() < 2400 ? value.getFullYear() + 543 : value.getFullYear();
return value.getDate() + " เดือน " + Extension.ToThaiMonth(value.getMonth() + 1) + " พ.ศ. " + yy;
}
public static sumObjectValues(array: any, propertyName: any) {
let sum = 0;
for (let i = 0; i < array.length; i++) {
if (array[i][propertyName] !== undefined) {
sum += array[i][propertyName];
}
}
return sum;
}
public static ToThaiShortDate(value: Date) {
let yy = value.getFullYear() < 2400 ? value.getFullYear() + 543 : value.getFullYear();
return (
value.getDate() +
" " +
Extension.ToThaiShortMonth(value.getMonth() + 1) +
" " +
yy.toString().slice(-2)
);
}
}
export default Extension;