2026-08-04 14:42:28 +07:00
|
|
|
const DateTimeLocal = {
|
|
|
|
|
mounted() {
|
|
|
|
|
this.localTime = this.el.querySelector("input[type=datetime-local]");
|
|
|
|
|
this.utcTime = this.el.querySelector("input[type=hidden]");
|
|
|
|
|
this.syncLocalTime();
|
|
|
|
|
|
|
|
|
|
this.handleInput = ({ target }) => {
|
|
|
|
|
if (target === this.localTime) this.syncUtcTime();
|
|
|
|
|
};
|
|
|
|
|
this.el.addEventListener("input", this.handleInput);
|
|
|
|
|
this.el.addEventListener("change", this.handleInput);
|
|
|
|
|
},
|
|
|
|
|
updated() {
|
|
|
|
|
this.localTime = this.el.querySelector("input[type=datetime-local]");
|
|
|
|
|
this.utcTime = this.el.querySelector("input[type=hidden]");
|
|
|
|
|
this.syncLocalTime();
|
|
|
|
|
},
|
|
|
|
|
syncLocalTime() {
|
|
|
|
|
if (!this.utcTime.value) {
|
2026-08-06 16:16:33 +07:00
|
|
|
this.setValue(this.localTime, "");
|
2026-08-04 14:42:28 +07:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const value = this.utcTime.value.replace(" ", "T").replace(/Z$/, "");
|
|
|
|
|
const date = new Date(`${value}Z`);
|
|
|
|
|
|
|
|
|
|
if (!Number.isNaN(date.getTime())) {
|
|
|
|
|
const pad = (part) => String(part).padStart(2, "0");
|
2026-08-06 16:16:33 +07:00
|
|
|
this.setValue(
|
|
|
|
|
this.localTime,
|
2026-08-04 14:42:28 +07:00
|
|
|
`${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())}` +
|
2026-08-06 16:16:33 +07:00
|
|
|
`T${pad(date.getHours())}:${pad(date.getMinutes())}`,
|
|
|
|
|
);
|
2026-08-04 14:42:28 +07:00
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
syncUtcTime() {
|
|
|
|
|
if (!this.localTime.value) {
|
2026-08-06 16:16:33 +07:00
|
|
|
this.setValue(this.utcTime, "");
|
2026-08-04 14:42:28 +07:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const date = new Date(this.localTime.value);
|
|
|
|
|
|
|
|
|
|
if (!Number.isNaN(date.getTime())) {
|
2026-08-06 16:16:33 +07:00
|
|
|
this.setValue(this.utcTime, date.toISOString().slice(0, 19));
|
|
|
|
|
this.setValue(this.localTime, this.localTime.value);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
setValue(input, value) {
|
|
|
|
|
input.value = value;
|
|
|
|
|
if (value) {
|
|
|
|
|
input.setAttribute("value", value);
|
|
|
|
|
} else {
|
|
|
|
|
input.removeAttribute("value");
|
2026-08-04 14:42:28 +07:00
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
destroyed() {
|
|
|
|
|
this.el.removeEventListener("input", this.handleInput);
|
|
|
|
|
this.el.removeEventListener("change", this.handleInput);
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default DateTimeLocal;
|