fix: error Invalid time value
All checks were successful
Build & Deploy on Dev / build (push) Successful in 1m16s

This commit is contained in:
Warunee Tamkoo 2026-02-13 16:49:00 +07:00
parent 307be83574
commit 7029b18a97

View file

@ -252,10 +252,7 @@ class Extension {
public static CheckCitizen(value: string) { public static CheckCitizen(value: string) {
let citizen = value; let citizen = value;
if (citizen == null || citizen == "" || citizen == undefined) { if (citizen == null || citizen == "" || citizen == undefined) {
throw new HttpError( throw new HttpError(HttpStatus.NOT_FOUND, "กรุณากรอกข้อมูลรหัสบัตรประจำตัวประชาชน");
HttpStatus.NOT_FOUND,
"กรุณากรอกข้อมูลรหัสบัตรประจำตัวประชาชน",
);
} }
if (citizen.length !== 13) { if (citizen.length !== 13) {
throw new HttpError( throw new HttpError(
@ -281,10 +278,7 @@ class Extension {
const chkDigit = (11 - calStp2) % 10; const chkDigit = (11 - calStp2) % 10;
if (citizenIdDigits[12] !== chkDigit) { if (citizenIdDigits[12] !== chkDigit) {
throw new HttpError( throw new HttpError(HttpStatus.NOT_FOUND, "ข้อมูลรหัสบัตรประจำตัวประชาชนไม่ถูกต้อง");
HttpStatus.NOT_FOUND,
"ข้อมูลรหัสบัตรประจำตัวประชาชนไม่ถูกต้อง"
);
} }
return citizen; return citizen;
} }
@ -295,6 +289,8 @@ class Extension {
subtractYear: number = 0, subtractYear: number = 0,
): number { ): number {
if (appointDate == null || appointDate == undefined) return 0; if (appointDate == null || appointDate == undefined) return 0;
// Check for Invalid Date
if (isNaN(appointDate.getTime())) return 0;
const now = new Date(); const now = new Date();
if (now.getMonth() - appointDate.getMonth() >= 6) { if (now.getMonth() - appointDate.getMonth() >= 6) {
return now.getFullYear() - appointDate.getFullYear() + 1 + plusYear - subtractYear; return now.getFullYear() - appointDate.getFullYear() + 1 + plusYear - subtractYear;
@ -312,13 +308,20 @@ class Extension {
return years; return years;
} }
public static CalculateAgeStrV2(date: Date, plusYear: number = 0, subtractYear: number = 0, method?:string) { public static CalculateAgeStrV2(
date: Date,
plusYear: number = 0,
subtractYear: number = 0,
method?: string,
) {
if (date == null || date == undefined) return ""; if (date == null || date == undefined) return "";
// Check for Invalid Date
if (isNaN(date.getTime())) return "";
const currentDate = new Date(); const currentDate = new Date();
if (date > currentDate && method !== "GET") { if (date > currentDate && method !== "GET") {
throw new Error("วันเกิดต้องไม่มากกว่าวันที่ปัจจุบัน"); throw new Error("วันเกิดต้องไม่มากกว่าวันที่ปัจจุบัน");
}else if(date > currentDate && method === "GET"){ } else if (date > currentDate && method === "GET") {
return "" return "";
} }
let years = currentDate.getFullYear() - date.getFullYear(); let years = currentDate.getFullYear() - date.getFullYear();
@ -388,9 +391,13 @@ class Extension {
} }
public static toDateOnlyString(date: Date): string { public static toDateOnlyString(date: Date): string {
return date.getFullYear() + "-" + return (
String(date.getMonth() + 1).padStart(2, "0") + "-" + date.getFullYear() +
String(date.getDate()).padStart(2, "0"); "-" +
String(date.getMonth() + 1).padStart(2, "0") +
"-" +
String(date.getDate()).padStart(2, "0")
);
} }
} }