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