summaryrefslogtreecommitdiffstats
path: root/src/mixins/datetime.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/mixins/datetime.js')
-rw-r--r--src/mixins/datetime.js138
1 files changed, 138 insertions, 0 deletions
diff --git a/src/mixins/datetime.js b/src/mixins/datetime.js
new file mode 100644
index 0000000..ca2d8f0
--- /dev/null
+++ b/src/mixins/datetime.js
@@ -0,0 +1,138 @@
+import dayjs from "dayjs";
+
+/**
+ * DateTime Mixin
+ * Handled timezone and format
+ */
+export default {
+ data() {
+ return {
+ userTimezone: localStorage.timezone || "auto",
+ };
+ },
+
+ methods: {
+ /**
+ * Convert value to UTC
+ * @param {string | number | Date | dayjs.Dayjs} value Time
+ * value to convert
+ * @returns {dayjs.Dayjs} Converted time
+ */
+ toUTC(value) {
+ return dayjs.tz(value, this.timezone).utc().format();
+ },
+
+ /**
+ * Used for <input type="datetime" />
+ * @param {string | number | Date | dayjs.Dayjs} value Value to
+ * convert
+ * @returns {string} Datetime string
+ */
+ toDateTimeInputFormat(value) {
+ return this.datetimeFormat(value, "YYYY-MM-DDTHH:mm");
+ },
+
+ /**
+ * Return a given value in the format YYYY-MM-DD HH:mm:ss
+ * @param {any} value Value to format as date time
+ * @returns {string} Formatted string
+ */
+ datetime(value) {
+ return this.datetimeFormat(value, "YYYY-MM-DD HH:mm:ss");
+ },
+
+ /**
+ * Converts a Unix timestamp to a formatted date and time string.
+ * @param {number} value - The Unix timestamp to convert.
+ * @returns {string} The formatted date and time string.
+ */
+ unixToDateTime(value) {
+ return dayjs.unix(value).tz(this.timezone).format("YYYY-MM-DD HH:mm:ss");
+ },
+
+ /**
+ * Converts a Unix timestamp to a dayjs object.
+ * @param {number} value - The Unix timestamp to convert.
+ * @returns {dayjs.Dayjs} The dayjs object representing the given timestamp.
+ */
+ unixToDayjs(value) {
+ return dayjs.unix(value).tz(this.timezone);
+ },
+
+ /**
+ * Converts the given value to a dayjs object.
+ * @param {string} value - the value to be converted
+ * @returns {dayjs.Dayjs} a dayjs object in the timezone of this instance
+ */
+ toDayjs(value) {
+ return dayjs.utc(value).tz(this.timezone);
+ },
+
+ /**
+ * Get time for maintenance
+ * @param {string | number | Date | dayjs.Dayjs} value Time to
+ * format
+ * @returns {string} Formatted string
+ */
+ datetimeMaintenance(value) {
+ const inputDate = new Date(value);
+ const now = new Date(Date.now());
+
+ if (inputDate.getFullYear() === now.getUTCFullYear() && inputDate.getMonth() === now.getUTCMonth() && inputDate.getDay() === now.getUTCDay()) {
+ return this.datetimeFormat(value, "HH:mm");
+ } else {
+ return this.datetimeFormat(value, "YYYY-MM-DD HH:mm");
+ }
+ },
+
+ /**
+ * Return a given value in the format YYYY-MM-DD
+ * @param {any} value Value to format as date
+ * @returns {string} Formatted string
+ */
+ 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} Formatted string
+ */
+ 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} Formatted string
+ */
+ 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;
+ },
+ }
+
+};