From 11631b733bae884a0205bca91ef5cc9f9bca0def Mon Sep 17 00:00:00 2001 From: waruneeauy Date: Wed, 16 Jul 2025 15:09:14 +0700 Subject: [PATCH] =?UTF-8?q?=E0=B9=81=E0=B8=81=E0=B9=89=20error=20Undefined?= =?UTF-8?q?=20NaN=20=E0=B8=82=E0=B8=AD=E0=B8=87=20month-picker?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/stores/mixin.ts | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/src/stores/mixin.ts b/src/stores/mixin.ts index c7650da..421dbe6 100644 --- a/src/stores/mixin.ts +++ b/src/stores/mixin.ts @@ -225,7 +225,13 @@ export const useCounterMixin = defineStore("mixin", () => { } function monthYear2Thai(month: number, year: number, isFullMonth = false) { - const date = new Date(`${year}-${month + 1}-1`); + if ( + month < 0 || + month > 11 || + !Number.isFinite(month) || + !Number.isFinite(year) + ) + return ""; const fullMonthThai = [ "มกราคม", "กุมภาพันธ์", @@ -254,19 +260,12 @@ export const useCounterMixin = defineStore("mixin", () => { "พ.ย.", "ธ.ค.", ]; - let dstYear = 0; - if (date.getFullYear() > 2500) { - dstYear = date.getFullYear(); - } else { - dstYear = date.getFullYear() + 543; - } - let dstMonth = ""; - if (isFullMonth) { - dstMonth = fullMonthThai[date.getMonth()]; - } else { - dstMonth = abbrMonthThai[date.getMonth()]; - } - return dstMonth + " " + dstYear; + + // assume year is in BE if > 2500 + let dstYear = year > 2500 ? year : year + 543; + // month is already 0-based + let dstMonth = isFullMonth ? fullMonthThai[month] : abbrMonthThai[month]; + return `${dstMonth} ${dstYear}`; } function dateToISO(date: Date) {