แก้ไขฟังก์ชั่นคำนวนจำนวนวัน เดือน ปี

This commit is contained in:
Warunee Tamkoo 2023-11-10 11:04:33 +07:00
parent 491c20b30e
commit d0894b891d

View file

@ -68,6 +68,13 @@ export const useCounterMixin = defineStore("mixin", () => {
return `${yearAge} ${year} ${monthAge} ${month} ${dateAge} ${day}`; 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( function date2Thai(
srcDate: Date, srcDate: Date,
isFullMonth: boolean = false, isFullMonth: boolean = false,
@ -835,7 +842,7 @@ export const useCounterMixin = defineStore("mixin", () => {
/** /**
* convert arabicNumberToText * convert arabicNumberToText
* @param Number * @param Number
* @returns format * @returns format
*/ */
function CheckNumber(Number: any) { 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) { if (startDate && endDate) {
const start = new Date(startDate); const start = new Date(startDate);
const end = new Date(endDate); const end = new Date(endDate);
const duration = end.getTime() - start.getTime(); // ระยะเวลาในมิลลิวินาที //Get the Timestamp
const date1_time_stamp = start.getTime();
const days = Math.floor(duration / (1000 * 60 * 60 * 24)); const date2_time_stamp = end.getTime();
const months = Math.floor(duration / (1000 * 60 * 60 * 24 * 30.44));
const years = Math.floor(duration / (1000 * 60 * 60 * 24 * 30.44 * 12)); let calc;
// return `${days} วัน, ${months} เดือน, ${years} ปี`; //Check which timestamp is greater
return `${years} ปี, ${months} เดือน, ${days} วัน`; 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 "";
} }
return { return {
calAge, calAge,
@ -981,6 +1031,6 @@ export const useCounterMixin = defineStore("mixin", () => {
dialogConfirm, dialogConfirm,
dialogRemove, dialogRemove,
arabicNumberToText, arabicNumberToText,
calculateDurationYmd calculateDurationYmd,
}; };
}); });