diff --git a/src/stores/mixin.ts b/src/stores/mixin.ts index 6bd56c2..32d167a 100644 --- a/src/stores/mixin.ts +++ b/src/stores/mixin.ts @@ -68,6 +68,13 @@ export const useCounterMixin = defineStore("mixin", () => { return `${yearAge} ${year} ${monthAge} ${month} ${dateAge} ${day}`; }; + /** + * ฟังก์ชั่นแปลงวันที่หรือวันที่และเวลาเป็น format ภาษาไทย เช่น 03 พ.ย. 2566 หรือ 03 พ.ย. 2566 11:00น. หรือแสดงเดือนแบบเต็มได้ + * @param srcDate ข้อมูลวันที่หรือวันที่และเวลา 2023-11-03 + * @param isFullMonth แสดงเดือนแบบเต็มไหม true คือแสดงเดือนแบบเต็ม, false คือแสดงแบบย่อ + * @param isTime แสดงเวลาไหม true คือแสดง, false คือไม่แสดง + * @returns + */ function date2Thai( srcDate: Date, isFullMonth: boolean = false, @@ -835,7 +842,7 @@ export const useCounterMixin = defineStore("mixin", () => { /** * ฟังก์ชั่นเช็คและแปลงตัวเลขก่อนส่งไป convert เป็นข้อความที่ฟังก์ชั่น arabicNumberToText - * @param Number จำนวนเงินเดือน + * @param Number จำนวนเงินเดือน * @returns ตัวเลขที่ถูกแปลง format แล้ว */ function CheckNumber(Number: any) { @@ -931,23 +938,66 @@ export const useCounterMixin = defineStore("mixin", () => { } } - function calculateDurationYmd(startDate: string | null, endDate: string | null) { + /** + * ฟังก์ชั่นคำนวนจำนวน ปี เดือน วัน + * @param startDate วันเริ่มต้น format MM-DD-YYYY" + * @param endDate วันสิ้นสุด format MM-DD-YYYY" + * @returns ผลการคำนวน ปี เดือน วัน ในรูปแบบ 1 ปี 10 เดือน 5 วัน + */ + function calculateDurationYmd(startDate: string, endDate: string) { if (startDate && endDate) { const start = new Date(startDate); const end = new Date(endDate); - - const duration = end.getTime() - start.getTime(); // ระยะเวลาในมิลลิวินาที - - const days = Math.floor(duration / (1000 * 60 * 60 * 24)); - const months = Math.floor(duration / (1000 * 60 * 60 * 24 * 30.44)); - const years = Math.floor(duration / (1000 * 60 * 60 * 24 * 30.44 * 12)); - - // return `${days} วัน, ${months} เดือน, ${years} ปี`; - return `${years} ปี, ${months} เดือน, ${days} วัน`; + + //Get the Timestamp + const date1_time_stamp = start.getTime(); + const date2_time_stamp = end.getTime(); + + let calc; + + //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); + } + + //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])) - 1); + 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 yrsTxt = "ปี"; + const mnthsTxt = "เดือน"; + const daysTxt = "วัน"; + + //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 : ""); + + return result.trim(); } - + return ""; - } + } return { calAge, @@ -981,6 +1031,6 @@ export const useCounterMixin = defineStore("mixin", () => { dialogConfirm, dialogRemove, arabicNumberToText, - calculateDurationYmd + calculateDurationYmd, }; });