2021-08-17 08:41:12 +00:00
|
|
|
import dayjs from "dayjs";
|
|
|
|
|
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: {
|
2023-01-05 22:55:51 +00:00
|
|
|
/**
|
|
|
|
* Convert value to UTC
|
|
|
|
* @param {string | number | Date | dayjs.Dayjs} value
|
|
|
|
* @returns {dayjs.Dayjs}
|
|
|
|
*/
|
2022-01-24 21:33:15 +00:00
|
|
|
toUTC(value) {
|
|
|
|
return dayjs.tz(value, this.timezone).utc().format();
|
|
|
|
},
|
|
|
|
|
2022-09-23 18:33:29 +00:00
|
|
|
/**
|
|
|
|
* Used for <input type="datetime" />
|
|
|
|
* @param value
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
toDateTimeInputFormat(value) {
|
|
|
|
return this.datetimeFormat(value, "YYYY-MM-DDTHH:mm");
|
|
|
|
},
|
|
|
|
|
2022-06-02 09:38:17 +00:00
|
|
|
/**
|
|
|
|
* Return a given value in the format YYYY-MM-DD HH:mm:ss
|
|
|
|
* @param {any} value Value to format as date time
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
2021-08-17 08:41:12 +00:00
|
|
|
datetime(value) {
|
|
|
|
return this.datetimeFormat(value, "YYYY-MM-DD HH:mm:ss");
|
|
|
|
},
|
|
|
|
|
2023-01-05 22:55:51 +00:00
|
|
|
/**
|
|
|
|
* Get time for maintenance
|
|
|
|
* @param {string | number | Date | dayjs.Dayjs} value
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
2022-01-23 14:22:00 +00:00
|
|
|
datetimeMaintenance(value) {
|
|
|
|
const inputDate = new Date(value);
|
|
|
|
const now = new Date(Date.now());
|
|
|
|
|
2022-04-30 12:57:08 +00:00
|
|
|
if (inputDate.getFullYear() === now.getUTCFullYear() && inputDate.getMonth() === now.getUTCMonth() && inputDate.getDay() === now.getUTCDay()) {
|
2022-01-24 21:33:15 +00:00
|
|
|
return this.datetimeFormat(value, "HH:mm");
|
2022-04-30 12:57:08 +00:00
|
|
|
} else {
|
2022-01-24 21:33:15 +00:00
|
|
|
return this.datetimeFormat(value, "YYYY-MM-DD HH:mm");
|
2022-04-30 12:57:08 +00:00
|
|
|
}
|
2022-01-23 14:22:00 +00:00
|
|
|
},
|
|
|
|
|
2022-06-02 09:38:17 +00:00
|
|
|
/**
|
|
|
|
* Return a given value in the format YYYY-MM-DD
|
|
|
|
* @param {any} value Value to format as date
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
2021-08-17 08:41:12 +00:00
|
|
|
date(value) {
|
|
|
|
return this.datetimeFormat(value, "YYYY-MM-DD");
|
|
|
|
},
|
|
|
|
|
2022-06-02 09:38:17 +00:00
|
|
|
/**
|
|
|
|
* Return a given value in the format HH:mm or if second is set
|
|
|
|
* to true, HH:mm:ss
|
|
|
|
* @param {any} value Value to format
|
|
|
|
* @param {boolean} second Should seconds be included?
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
2021-08-17 08:41:12 +00:00
|
|
|
time(value, second = true) {
|
|
|
|
let secondString;
|
|
|
|
if (second) {
|
|
|
|
secondString = ":ss";
|
|
|
|
} else {
|
|
|
|
secondString = "";
|
|
|
|
}
|
|
|
|
return this.datetimeFormat(value, "HH:mm" + secondString);
|
|
|
|
},
|
|
|
|
|
2022-06-02 09:38:17 +00:00
|
|
|
/**
|
|
|
|
* Return a value in a custom format
|
|
|
|
* @param {any} value Value to format
|
|
|
|
* @param {any} format Format to return value in
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
2021-08-17 08:41:12 +00:00
|
|
|
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
|
|
|
},
|
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
|
|
|
};
|