2021-12-10 21:12:06 +08:00
|
|
|
<template>
|
2021-12-15 19:22:24 +08:00
|
|
|
<div class="date-time-picker">
|
2022-02-18 18:25:51 +08:00
|
|
|
<DatePicker v-if="!timeOnly" @change="updateDate" :selectorId="`${this.selectorId}_Date`" :defaultValue="defaultValue" />
|
|
|
|
<TimePicker v-if="!dateOnly" @change="updateTime" :selectorId="`${this.selectorId}_Time`" :defaultValue="getTime(defaultValue)" />
|
2021-12-10 21:12:06 +08:00
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
2021-12-15 19:22:24 +08:00
|
|
|
import TimePicker from 'vue/shared/time_picker.vue'
|
|
|
|
import DatePicker from 'vue/shared/date_picker.vue'
|
|
|
|
|
2021-12-10 21:12:06 +08:00
|
|
|
export default {
|
|
|
|
name: 'DateTimePicker',
|
|
|
|
props: {
|
2021-12-15 19:22:24 +08:00
|
|
|
dateOnly: { type: Boolean, default: false },
|
|
|
|
timeOnly: { type: Boolean, default: false },
|
2022-02-18 18:25:51 +08:00
|
|
|
selectorId: { type: String, required: true },
|
|
|
|
defaultValue: {type: Date, required: false }
|
2021-12-10 21:12:06 +08:00
|
|
|
},
|
2021-12-15 19:22:24 +08:00
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
date: '',
|
|
|
|
time: '',
|
|
|
|
datetime: ''
|
|
|
|
}
|
|
|
|
},
|
|
|
|
components: {
|
|
|
|
TimePicker,
|
|
|
|
DatePicker
|
2021-12-10 21:12:06 +08:00
|
|
|
},
|
|
|
|
methods: {
|
2021-12-15 19:22:24 +08:00
|
|
|
updateDate(value) {
|
|
|
|
this.date = value;
|
|
|
|
this.updateDateTime();
|
|
|
|
},
|
2022-02-18 18:25:51 +08:00
|
|
|
getTime(dateTime) {
|
|
|
|
return `${dateTime.getHours()}:${dateTime.getMinutes()}`
|
|
|
|
},
|
2021-12-15 19:22:24 +08:00
|
|
|
updateTime(value) {
|
|
|
|
this.time = value;
|
|
|
|
this.updateDateTime();
|
|
|
|
},
|
|
|
|
updateDateTime() {
|
|
|
|
this.recalcTimestamp();
|
|
|
|
this.$emit('change', this.datetime);
|
|
|
|
},
|
|
|
|
|
|
|
|
isValidTime() {
|
|
|
|
return /^([0-9]|0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]$/.test(this.time);
|
|
|
|
},
|
|
|
|
isValidDate() {
|
|
|
|
return (this.date instanceof Date) && !isNaN(this.date.getTime());
|
|
|
|
},
|
|
|
|
recalcTimestamp() {
|
|
|
|
let date = this.timeOnly ? new Date() : this.date;
|
2022-02-21 18:10:50 +08:00
|
|
|
if (this.isValidDate(date)) {
|
|
|
|
if (!this.isValidTime()) {
|
|
|
|
date.setHours(0);
|
|
|
|
date.setMinutes(0);
|
|
|
|
} else {
|
|
|
|
date.setHours(this.time.split(':')[0]);
|
|
|
|
date.setMinutes(this.time.split(':')[1]);
|
|
|
|
}
|
|
|
|
this.datetime = date
|
2021-12-15 19:22:24 +08:00
|
|
|
} else {
|
2022-02-21 18:10:50 +08:00
|
|
|
this.datetime = null;
|
2021-12-15 19:22:24 +08:00
|
|
|
}
|
2021-12-10 21:12:06 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-12-15 19:22:24 +08:00
|
|
|
</script>
|