From 886782921195fb79c3c590e7590c94dd1e31a843 Mon Sep 17 00:00:00 2001 From: "DESKTOP-1R2VSQH\\Lenovo ThinkPad E490" Date: Wed, 21 Jan 2026 13:14:33 +0700 Subject: [PATCH] fix:formatDate --- .../components/2_InvestigateFacts/Form.vue | 8 +- .../3_InvestigateDisciplinary/Form.vue | 8 +- src/stores/mixin.ts | 74 ++++++------------- 3 files changed, 36 insertions(+), 54 deletions(-) diff --git a/src/modules/11_discipline/components/2_InvestigateFacts/Form.vue b/src/modules/11_discipline/components/2_InvestigateFacts/Form.vue index b8e6e5b48..2eb919bb2 100644 --- a/src/modules/11_discipline/components/2_InvestigateFacts/Form.vue +++ b/src/modules/11_discipline/components/2_InvestigateFacts/Form.vue @@ -430,8 +430,12 @@ function changeFormData() { /** เช็ควันที่สิ้นสุดต้องมากกว่า หรือเท่ากับวันเริ่ม */ function changeFormDataDate() { isSave.value = true; - const startDate = new Date(formData.investigationDateStart as Date); - const endDate = new Date(formData.investigationDateEnd as Date); + const startDate = moment( + new Date(formData.investigationDateStart as Date) + ).startOf("day"); + const endDate = moment( + new Date(formData.investigationDateEnd as Date) + ).startOf("day"); if (startDate > endDate) { formData.investigationDateEnd = null; diff --git a/src/modules/11_discipline/components/3_InvestigateDisciplinary/Form.vue b/src/modules/11_discipline/components/3_InvestigateDisciplinary/Form.vue index 1afb7673e..60d0d8066 100644 --- a/src/modules/11_discipline/components/3_InvestigateDisciplinary/Form.vue +++ b/src/modules/11_discipline/components/3_InvestigateDisciplinary/Form.vue @@ -556,8 +556,12 @@ function changeFormData() { function changeFormDataDate() { isSave.value = true; isSaveInfo.value = true; - const startDate = new Date(formData.disciplinaryDateStart as Date); - const endDate = new Date(formData.disciplinaryDateEnd as Date); + const startDate = moment( + new Date(formData.disciplinaryDateStart as Date) + ).startOf("day"); + const endDate = moment( + new Date(formData.disciplinaryDateEnd as Date) + ).startOf("day"); if (startDate > endDate) { formData.disciplinaryDateEnd = null; diff --git a/src/stores/mixin.ts b/src/stores/mixin.ts index 3284fd227..4ea752184 100644 --- a/src/stores/mixin.ts +++ b/src/stores/mixin.ts @@ -943,65 +943,39 @@ export const useCounterMixin = defineStore("mixin", () => { * @returns ผลการคำนวน ปี เดือน วัน ในรูปแบบ 1 ปี 10 เดือน 5 วัน */ function calculateDurationYmd(startDate: any, endDate: any) { - if (startDate && endDate) { - const start = new Date(startDate); - const end = new Date(endDate); + if (!startDate || !endDate) return ""; - //Get the Timestamp - const date1_time_stamp = start.getTime(); - const date2_time_stamp = end.getTime(); + let start = moment(startDate).startOf("day"); + let end = moment(endDate).startOf("day"); - let calc; + // สลับค่าเพื่อให้ end มากกว่า start เสมอ + if (start > end) [start, end] = [end, start]; - //Check which timestamp is greater - if (date1_time_stamp > date2_time_stamp) { - calc = new Date(date1_time_stamp - date2_time_stamp); - } else { - calc = new Date(date2_time_stamp - date1_time_stamp); - } + const years = end.diff(start, "years"); + start.add(years, "years"); - //retrieve the date, month and year - const calcFormatTmp = - calc.getDate() + "-" + (calc.getMonth() + 1) + "-" + calc.getFullYear(); - //Convert to an array and store - const calcFormat = calcFormatTmp.split("-"); - //Subtract each member of our array from the default date - const days_passed = Number(Math.abs(Number(calcFormat[0]))); - const months_passed = Number(Math.abs(Number(calcFormat[1])) - 1); - const years_passed = Number(Math.abs(Number(calcFormat[2])) - 1970); + const months = end.diff(start, "months"); + start.add(months, "months"); - //Set up custom text - const yrsTxt = "ปี"; - const mnthsTxt = "เดือน"; - const daysTxt = "วัน"; + const days = end.diff(start, "days"); - //display result with custom text - const result = - (years_passed > 0 && (months_passed > 0 || days_passed > 0) - ? years_passed + " " + yrsTxt + ", " - : "") + - (years_passed > 0 && months_passed == 0 && days_passed == 0 - ? years_passed + " " + yrsTxt + " " - : "") + - (months_passed > 0 && days_passed > 0 - ? months_passed + " " + mnthsTxt + ", " - : "") + - (months_passed > 0 && days_passed == 0 - ? months_passed + " " + mnthsTxt + " " - : "") + - (days_passed > 0 ? days_passed + " " + daysTxt : ""); + // การแสดงผล + const result = []; + if (years > 0) result.push(`${years} ปี`); + if (months > 0) result.push(`${months} เดือน`); + if (days > 0) result.push(`${days} วัน`); - return result.trim(); - } - - return ""; + return result.length > 0 ? result.join(" ") : "0 วัน"; } function diffDay(startDate: any, endDate: any) { - var d1 = moment(startDate); - var d2 = moment(endDate); - const daydiff = Math.ceil(moment.duration(d2.diff(d1)).asDays()); - return daydiff; + if (!startDate || !endDate) return 0; + + const start = moment(startDate).startOf("day"); + const end = moment(endDate).startOf("day"); + + // คืนค่าความต่างเป็นจำนวนวัน (ตัวเลขจำนวนเต็ม) + return end.diff(start, "days"); } function findOrgName(obj: any) { @@ -1536,6 +1510,6 @@ export const useCounterMixin = defineStore("mixin", () => { findOrgNameOldHtml, findOrgChildNameHtml, findChildNameHtml, - notifyWarring + notifyWarring, }; });