2021-08-17 08:41:12 +00:00
|
|
|
import dayjs from "dayjs";
|
|
|
|
import relativeTime from "dayjs/plugin/relativeTime";
|
2021-09-28 06:07:42 +00:00
|
|
|
import timezone from "dayjs/plugin/timezone";
|
|
|
|
import utc from "dayjs/plugin/utc";
|
2021-08-17 08:41:12 +00:00
|
|
|
dayjs.extend(utc);
|
|
|
|
dayjs.extend(timezone);
|
|
|
|
dayjs.extend(relativeTime);
|
|
|
|
|
2021-08-17 08:43:59 +00:00
|
|
|
/**
|
|
|
|
* DateTime Mixin
|
|
|
|
* Handled timezone and format
|
|
|
|
*/
|
2021-08-17 08:41:12 +00:00
|
|
|
export default {
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
userTimezone: localStorage.timezone || "auto",
|
2021-09-28 06:07:42 +00:00
|
|
|
};
|
2021-08-17 08:41:12 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
methods: {
|
|
|
|
datetime(value) {
|
|
|
|
return this.datetimeFormat(value, "YYYY-MM-DD HH:mm:ss");
|
|
|
|
},
|
|
|
|
|
2022-01-23 14:22:00 +00:00
|
|
|
datetimeMaintenance(value) {
|
|
|
|
const inputDate = new Date(value);
|
|
|
|
const now = new Date(Date.now());
|
|
|
|
|
|
|
|
if (inputDate.getFullYear() === now.getFullYear() && inputDate.getMonth() === now.getMonth() && inputDate.getDay() === now.getDay())
|
|
|
|
return this.datetimeMaintenanceFormat(value, "HH:mm");
|
|
|
|
else
|
|
|
|
return this.datetimeMaintenanceFormat(value, "YYYY-MM-DD HH:mm");
|
|
|
|
},
|
|
|
|
|
2021-08-17 08:41:12 +00:00
|
|
|
date(value) {
|
|
|
|
return this.datetimeFormat(value, "YYYY-MM-DD");
|
|
|
|
},
|
|
|
|
|
|
|
|
time(value, second = true) {
|
|
|
|
let secondString;
|
|
|
|
if (second) {
|
|
|
|
secondString = ":ss";
|
|
|
|
} else {
|
|
|
|
secondString = "";
|
|
|
|
}
|
|
|
|
return this.datetimeFormat(value, "HH:mm" + secondString);
|
|
|
|
},
|
|
|
|
|
|
|
|
datetimeFormat(value, format) {
|
|
|
|
if (value !== undefined && value !== "") {
|
|
|
|
return dayjs.utc(value).tz(this.timezone).format(format);
|
|
|
|
}
|
|
|
|
return "";
|
2022-01-23 14:22:00 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
datetimeMaintenanceFormat(value, format) {
|
|
|
|
if (value !== undefined && value !== "") {
|
|
|
|
return dayjs(value).format(format);
|
|
|
|
}
|
|
|
|
return "";
|
2021-08-17 08:41:12 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
computed: {
|
|
|
|
timezone() {
|
|
|
|
if (this.userTimezone === "auto") {
|
2021-09-28 06:07:42 +00:00
|
|
|
return dayjs.tz.guess();
|
2021-08-17 08:41:12 +00:00
|
|
|
}
|
|
|
|
|
2021-09-28 06:07:42 +00:00
|
|
|
return this.userTimezone;
|
2021-08-17 08:41:12 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-09-28 06:07:42 +00:00
|
|
|
};
|