2021-12-10 21:12:06 +08:00
|
|
|
<template>
|
2023-08-28 19:10:02 +08:00
|
|
|
<div class="date-time-picker grow">
|
2023-08-23 17:59:21 +08:00
|
|
|
<DatePicker v-if="!timeOnly"
|
|
|
|
@change="updateDate"
|
|
|
|
:placeholder="placeholder"
|
|
|
|
:selectorId="`${this.selectorId}_Date`"
|
|
|
|
:defaultValue="defaultValue"
|
|
|
|
/>
|
|
|
|
<TimePicker v-if="!dateOnly"
|
|
|
|
@change="updateTime"
|
|
|
|
:placeholder="placeholder"
|
|
|
|
:selectorId="`${this.selectorId}_Time`"
|
|
|
|
:defaultValue="getTime(defaultValue)"
|
|
|
|
/>
|
2021-12-10 21:12:06 +08:00
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
2023-03-30 19:27:10 +08:00
|
|
|
import TimePicker from './time_picker.vue'
|
|
|
|
import DatePicker from './date_picker.vue'
|
2021-12-15 19:22:24 +08:00
|
|
|
|
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 },
|
2023-08-23 17:59:21 +08:00
|
|
|
defaultValue: { type: Date, required: false },
|
|
|
|
placeholder: { type: String }
|
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) {
|
2023-07-05 14:30:37 +08:00
|
|
|
if(!this.isValidDate(dateTime)) return
|
2022-02-25 22:27:31 +08:00
|
|
|
return `${dateTime.getHours().toString().padStart(2, '0')}:${dateTime.getMinutes().toString().padStart(2, '0')}`
|
2022-02-18 18:25:51 +08:00
|
|
|
},
|
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);
|
|
|
|
},
|
2022-02-23 16:24:11 +08:00
|
|
|
isValidDate(date) {
|
|
|
|
return (date instanceof Date) && !isNaN(date.getTime());
|
2021-12-15 19:22:24 +08:00
|
|
|
},
|
|
|
|
recalcTimestamp() {
|
|
|
|
let date = this.timeOnly ? new Date() : this.date;
|
2022-02-23 21:55:04 +08:00
|
|
|
if (this.isValidDate(date) && (this.dateOnly || this.isValidTime())) {
|
|
|
|
if (this.dateOnly) {
|
2022-02-21 18:10:50 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-02-25 22:27:31 +08:00
|
|
|
</script>
|