uptime-kuma/src/mixins/datetime.js

81 lines
2.1 KiB
JavaScript
Raw Normal View History

2021-08-17 16:41:12 +08:00
import dayjs from "dayjs";
import relativeTime from "dayjs/plugin/relativeTime";
import timezone from "dayjs/plugin/timezone";
import utc from "dayjs/plugin/utc";
2021-08-17 16:41:12 +08:00
dayjs.extend(utc);
dayjs.extend(timezone);
dayjs.extend(relativeTime);
2021-08-17 16:43:59 +08:00
/**
* DateTime Mixin
* Handled timezone and format
*/
2021-08-17 16:41:12 +08:00
export default {
data() {
return {
userTimezone: localStorage.timezone || "auto",
};
2021-08-17 16:41:12 +08:00
},
methods: {
/**
* 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 16:41:12 +08:00
datetime(value) {
return this.datetimeFormat(value, "YYYY-MM-DD HH:mm:ss");
},
/**
* Return a given value in the format YYYY-MM-DD
* @param {any} value Value to format as date
* @returns {string}
*/
2021-08-17 16:41:12 +08:00
date(value) {
return this.datetimeFormat(value, "YYYY-MM-DD");
},
/**
* 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 16:41:12 +08:00
time(value, second = true) {
let secondString;
if (second) {
secondString = ":ss";
} else {
secondString = "";
}
return this.datetimeFormat(value, "HH:mm" + secondString);
},
/**
* 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 16:41:12 +08:00
datetimeFormat(value, format) {
if (value !== undefined && value !== "") {
return dayjs.utc(value).tz(this.timezone).format(format);
}
return "";
}
},
computed: {
timezone() {
if (this.userTimezone === "auto") {
return dayjs.tz.guess();
2021-08-17 16:41:12 +08:00
}
return this.userTimezone;
2021-08-17 16:41:12 +08:00
},
}
};