create datetime mixin

This commit is contained in:
LouisLam 2021-08-17 16:41:12 +08:00
parent 61e758d872
commit 5e52f230b1
4 changed files with 60 additions and 20 deletions

View File

@ -22,15 +22,11 @@ export default {
computed: { computed: {
displayText() { displayText() {
if (this.value !== undefined && this.value !== "") { if (this.dateOnly) {
let format = "YYYY-MM-DD HH:mm:ss"; return this.$root.date(this.value);
if (this.dateOnly) { } else {
format = "YYYY-MM-DD"; return this.$root.datetime(this.value);
}
return dayjs.utc(this.value).tz(this.$root.timezone).format(format);
} }
return "";
}, },
}, },
} }

View File

@ -11,6 +11,7 @@ import Layout from "./layouts/Layout.vue";
import socket from "./mixins/socket"; import socket from "./mixins/socket";
import theme from "./mixins/theme"; import theme from "./mixins/theme";
import mobile from "./mixins/mobile"; import mobile from "./mixins/mobile";
import datetime from "./mixins/datetime";
import Dashboard from "./pages/Dashboard.vue"; import Dashboard from "./pages/Dashboard.vue";
import DashboardHome from "./pages/DashboardHome.vue"; import DashboardHome from "./pages/DashboardHome.vue";
import Details from "./pages/Details.vue"; import Details from "./pages/Details.vue";
@ -80,7 +81,8 @@ const app = createApp({
mixins: [ mixins: [
socket, socket,
theme, theme,
mobile mobile,
datetime
], ],
data() { data() {
return { return {

53
src/mixins/datetime.js Normal file
View File

@ -0,0 +1,53 @@
import dayjs from "dayjs";
import utc from "dayjs/plugin/utc";
import timezone from "dayjs/plugin/timezone";
import relativeTime from "dayjs/plugin/relativeTime";
dayjs.extend(utc);
dayjs.extend(timezone);
dayjs.extend(relativeTime);
export default {
data() {
return {
userTimezone: localStorage.timezone || "auto",
}
},
methods: {
datetime(value) {
return this.datetimeFormat(value, "YYYY-MM-DD HH:mm:ss");
},
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 "";
}
},
computed: {
timezone() {
if (this.userTimezone === "auto") {
return dayjs.tz.guess()
}
return this.userTimezone
},
}
}

View File

@ -1,4 +1,3 @@
import dayjs from "dayjs";
import { io } from "socket.io-client"; import { io } from "socket.io-client";
import { useToast } from "vue-toastification"; import { useToast } from "vue-toastification";
const toast = useToast() const toast = useToast()
@ -17,7 +16,6 @@ export default {
connectCount: 0, connectCount: 0,
}, },
remember: (localStorage.remember !== "0"), remember: (localStorage.remember !== "0"),
userTimezone: localStorage.timezone || "auto",
allowLoginDialog: false, // Allowed to show login dialog, but "loggedIn" have to be true too. This exists because prevent the login dialog show 0.1s in first before the socket server auth-ed. allowLoginDialog: false, // Allowed to show login dialog, but "loggedIn" have to be true too. This exists because prevent the login dialog show 0.1s in first before the socket server auth-ed.
loggedIn: false, loggedIn: false,
monitorList: { }, monitorList: { },
@ -265,15 +263,6 @@ export default {
computed: { computed: {
timezone() {
if (this.userTimezone === "auto") {
return dayjs.tz.guess()
}
return this.userTimezone
},
lastHeartbeatList() { lastHeartbeatList() {
let result = {} let result = {}