Merge branch 'develop' into dev
* develop: fix:calculateDurationYmd
This commit is contained in:
commit
12d2b23ede
1 changed files with 16 additions and 45 deletions
|
|
@ -973,58 +973,29 @@ 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").add(1, "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 findOrgName(obj: any) {
|
function findOrgName(obj: any) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue