fix:formatDate
All checks were successful
Build & Deploy on Dev / build (push) Successful in 2m56s

This commit is contained in:
DESKTOP-1R2VSQH\Lenovo ThinkPad E490 2026-01-21 13:14:33 +07:00
parent 99bba05167
commit 8867829211
3 changed files with 36 additions and 54 deletions

View file

@ -430,8 +430,12 @@ function changeFormData() {
/** เช็ควันที่สิ้นสุดต้องมากกว่า หรือเท่ากับวันเริ่ม */ /** เช็ควันที่สิ้นสุดต้องมากกว่า หรือเท่ากับวันเริ่ม */
function changeFormDataDate() { function changeFormDataDate() {
isSave.value = true; isSave.value = true;
const startDate = new Date(formData.investigationDateStart as Date); const startDate = moment(
const endDate = new Date(formData.investigationDateEnd as Date); new Date(formData.investigationDateStart as Date)
).startOf("day");
const endDate = moment(
new Date(formData.investigationDateEnd as Date)
).startOf("day");
if (startDate > endDate) { if (startDate > endDate) {
formData.investigationDateEnd = null; formData.investigationDateEnd = null;

View file

@ -556,8 +556,12 @@ function changeFormData() {
function changeFormDataDate() { function changeFormDataDate() {
isSave.value = true; isSave.value = true;
isSaveInfo.value = true; isSaveInfo.value = true;
const startDate = new Date(formData.disciplinaryDateStart as Date); const startDate = moment(
const endDate = new Date(formData.disciplinaryDateEnd as Date); new Date(formData.disciplinaryDateStart as Date)
).startOf("day");
const endDate = moment(
new Date(formData.disciplinaryDateEnd as Date)
).startOf("day");
if (startDate > endDate) { if (startDate > endDate) {
formData.disciplinaryDateEnd = null; formData.disciplinaryDateEnd = null;

View file

@ -943,65 +943,39 @@ export const useCounterMixin = defineStore("mixin", () => {
* @returns 1 10 5 * @returns 1 10 5
*/ */
function calculateDurationYmd(startDate: any, endDate: any) { function calculateDurationYmd(startDate: any, endDate: any) {
if (startDate && endDate) { if (!startDate || !endDate) return "";
const start = new Date(startDate);
const end = new Date(endDate);
//Get the Timestamp let start = moment(startDate).startOf("day");
const date1_time_stamp = start.getTime(); let end = moment(endDate).startOf("day");
const date2_time_stamp = end.getTime();
let calc; // สลับค่าเพื่อให้ end มากกว่า start เสมอ
if (start > end) [start, end] = [end, start];
//Check which timestamp is greater const years = end.diff(start, "years");
if (date1_time_stamp > date2_time_stamp) { start.add(years, "years");
calc = new Date(date1_time_stamp - date2_time_stamp);
} else {
calc = new Date(date2_time_stamp - date1_time_stamp);
}
//retrieve the date, month and year const months = end.diff(start, "months");
const calcFormatTmp = start.add(months, "months");
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);
//Set up custom text const days = end.diff(start, "days");
const yrsTxt = "ปี";
const mnthsTxt = "เดือน";
const daysTxt = "วัน";
//display result with custom text // การแสดงผล
const result = const result = [];
(years_passed > 0 && (months_passed > 0 || days_passed > 0) if (years > 0) result.push(`${years} ปี`);
? years_passed + " " + yrsTxt + ", " if (months > 0) result.push(`${months} เดือน`);
: "") + if (days > 0) result.push(`${days} วัน`);
(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 : "");
return result.trim(); return result.length > 0 ? result.join(" ") : "0 วัน";
}
return "";
} }
function diffDay(startDate: any, endDate: any) { function diffDay(startDate: any, endDate: any) {
var d1 = moment(startDate); if (!startDate || !endDate) return 0;
var d2 = moment(endDate);
const daydiff = Math.ceil(moment.duration(d2.diff(d1)).asDays()); const start = moment(startDate).startOf("day");
return daydiff; const end = moment(endDate).startOf("day");
// คืนค่าความต่างเป็นจำนวนวัน (ตัวเลขจำนวนเต็ม)
return end.diff(start, "days");
} }
function findOrgName(obj: any) { function findOrgName(obj: any) {
@ -1536,6 +1510,6 @@ export const useCounterMixin = defineStore("mixin", () => {
findOrgNameOldHtml, findOrgNameOldHtml,
findOrgChildNameHtml, findOrgChildNameHtml,
findChildNameHtml, findChildNameHtml,
notifyWarring notifyWarring,
}; };
}); });