|
@@ -3,7 +3,7 @@
|
|
<van-field
|
|
<van-field
|
|
:label="label"
|
|
:label="label"
|
|
:placeholder="placeholder"
|
|
:placeholder="placeholder"
|
|
- v-model="dateTime"
|
|
|
|
|
|
+ v-model="currentDateTime"
|
|
readonly
|
|
readonly
|
|
@touchstart="clickHandler"
|
|
@touchstart="clickHandler"
|
|
/>
|
|
/>
|
|
@@ -21,14 +21,13 @@
|
|
</template>
|
|
</template>
|
|
|
|
|
|
<script lang="ts" setup>
|
|
<script lang="ts" setup>
|
|
-
|
|
|
|
defineOptions({
|
|
defineOptions({
|
|
name: 'DateTimeRange'
|
|
name: 'DateTimeRange'
|
|
})
|
|
})
|
|
const props = defineProps<{
|
|
const props = defineProps<{
|
|
label: string
|
|
label: string
|
|
placeholder: string
|
|
placeholder: string
|
|
- modelValue: string[]
|
|
|
|
|
|
+ modelValue: string
|
|
}>()
|
|
}>()
|
|
const emit = defineEmits<{
|
|
const emit = defineEmits<{
|
|
(e: 'update:modelValue', payload: any): void
|
|
(e: 'update:modelValue', payload: any): void
|
|
@@ -39,19 +38,19 @@ const currentDay: number = new Date().getDay();
|
|
const selectYear = ref<string>(currentYear.toString());
|
|
const selectYear = ref<string>(currentYear.toString());
|
|
const selectMonth = ref<string>(currentMonth.toString());
|
|
const selectMonth = ref<string>(currentMonth.toString());
|
|
|
|
|
|
-const dateTime = ref<number>()
|
|
|
|
|
|
+const currentDateTime = ref<string>()
|
|
const selectDateTime = ref<string[]>([])
|
|
const selectDateTime = ref<string[]>([])
|
|
-if(props.modelValue.length === 0){
|
|
|
|
- selectDateTime.value = [currentYear.toString(), currentMonth.toString(), currentDay.toString()]
|
|
|
|
|
|
+if (props.modelValue) {
|
|
|
|
+ selectDateTime.value = formatDateTimeArr(props.modelValue);
|
|
} else {
|
|
} else {
|
|
- const newVal = props.modelValue
|
|
|
|
- selectDateTime.value = [newVal[0], newVal[1], newVal[2], newVal[3], newVal[4]]
|
|
|
|
|
|
+ selectDateTime.value = [currentYear.toString(), currentMonth.toString(), currentDay.toString()]
|
|
}
|
|
}
|
|
watch(() => props.modelValue, (newVal) => {
|
|
watch(() => props.modelValue, (newVal) => {
|
|
- selectDateTime.value = newVal
|
|
|
|
- selectYear.value = newVal[0].toString()
|
|
|
|
- selectMonth.value = newVal[1].toString()
|
|
|
|
- setMonthByDay(newVal[0],newVal[1]);
|
|
|
|
|
|
+ const newArr = formatDateTimeArr(newVal);
|
|
|
|
+ selectDateTime.value = newArr
|
|
|
|
+ selectYear.value = newArr[0].toString()
|
|
|
|
+ selectMonth.value = newArr[1].toString()
|
|
|
|
+ setMonthByDay(newArr[0],newArr[1]);
|
|
})
|
|
})
|
|
/***
|
|
/***
|
|
* 固定统计年、月、时、分数据
|
|
* 固定统计年、月、时、分数据
|
|
@@ -68,7 +67,7 @@ const houseList = [];
|
|
for (let i = 1; i <= 12; i++){
|
|
for (let i = 1; i <= 12; i++){
|
|
houseList.push(fillZero(i));
|
|
houseList.push(fillZero(i));
|
|
}
|
|
}
|
|
-const minuteList = ['0', '30']
|
|
|
|
|
|
+const minuteList = ['00', '30']
|
|
|
|
|
|
const dateTimeRangeColumns = reactive([
|
|
const dateTimeRangeColumns = reactive([
|
|
yearList.map(val => {
|
|
yearList.map(val => {
|
|
@@ -104,6 +103,41 @@ function fillZero(num: number): string {
|
|
const num2 = Number(num);
|
|
const num2 = Number(num);
|
|
return num < 10 ? "0" + num2.toString() : num2.toString()
|
|
return num < 10 ? "0" + num2.toString() : num2.toString()
|
|
}
|
|
}
|
|
|
|
+/***
|
|
|
|
+ * 格式化YYYY-MM-DD HH:MM:SS转成[yyy, mm, dd, hh, mm, dd]
|
|
|
|
+ */
|
|
|
|
+function formatDateTimeArr(dateTime: string): string[] {
|
|
|
|
+ if (!dateTime){
|
|
|
|
+ console.warn('formatDateTimeArr方法参数dateTime值不能为空!')
|
|
|
|
+ return []
|
|
|
|
+ }
|
|
|
|
+ const dateTimeArr = dateTime.split(' ');
|
|
|
|
+ if (dateTimeArr.length < 2) {
|
|
|
|
+ throw new Error('dateTime格式不正确! 请符合:YYYY-MM-DD HH:MM:SS');
|
|
|
|
+ }
|
|
|
|
+ const dateArr: string[] = dateTimeArr[0].split('-');
|
|
|
|
+ const timeArr: string[] = dateTimeArr[1].split(':');
|
|
|
|
+ return dateArr.concat(timeArr) as string[];
|
|
|
|
+}
|
|
|
|
+/**
|
|
|
|
+ * 格式化[yyy, mm, dd, hh, mm, dd]转成 YYYY-MM-DD HH:MM:SS
|
|
|
|
+ **/
|
|
|
|
+function formatDateTimeByArr(arr: string[]): string {
|
|
|
|
+ if (!arr || arr.length === 0) {
|
|
|
|
+ console.warn('formatDateTimeByArr方法参数arr值不能为空!')
|
|
|
|
+ return ''
|
|
|
|
+ }
|
|
|
|
+ const dateArr: string[] = []
|
|
|
|
+ arr[0] ? dateArr.push(arr[0]) : null
|
|
|
|
+ arr[1] ? dateArr.push(arr[1]) : null
|
|
|
|
+ arr[2] ? dateArr.push(arr[2]) : null
|
|
|
|
+
|
|
|
|
+ const timeArr: string[] = []
|
|
|
|
+ arr[3] ? timeArr.push(arr[3]) : null
|
|
|
|
+ arr[4] ? timeArr.push(arr[4]) : null
|
|
|
|
+ arr[5] ? timeArr.push(arr[5]) : null
|
|
|
|
+ return dateArr.join('-') +' ' + timeArr.join(':')
|
|
|
|
+}
|
|
setMonthByDay(currentYear, currentMonth);
|
|
setMonthByDay(currentYear, currentMonth);
|
|
const changeHandle = (val: any) => {
|
|
const changeHandle = (val: any) => {
|
|
const arr = val.selectedValues;
|
|
const arr = val.selectedValues;
|
|
@@ -111,10 +145,16 @@ const changeHandle = (val: any) => {
|
|
setMonthByDay(arr[0], arr[1]);
|
|
setMonthByDay(arr[0], arr[1]);
|
|
selectYear.value = arr[0];
|
|
selectYear.value = arr[0];
|
|
selectMonth.value = arr[1];
|
|
selectMonth.value = arr[1];
|
|
|
|
+ const dateTimeStr:string = formatDateTimeByArr(arr);
|
|
|
|
+ currentDateTime.value = dateTimeStr
|
|
|
|
+ emit('update:modelValue', dateTimeStr)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
const confirmHandle = () => {
|
|
const confirmHandle = () => {
|
|
- emit('update:modelValue', `${selectDateTime.value[0]}-${selectDateTime.value[1]}-${selectDateTime.value[2]} ${selectDateTime.value[3]}:${selectDateTime.value[4]}`)
|
|
|
|
|
|
+ const dateTimeStr:string = formatDateTimeByArr(selectDateTime.value);
|
|
|
|
+ currentDateTime.value = dateTimeStr
|
|
|
|
+ emit('update:modelValue', dateTimeStr)
|
|
|
|
+ popupShow.value = false;
|
|
}
|
|
}
|
|
const cancelHandle = () => {
|
|
const cancelHandle = () => {
|
|
popupShow.value = false;
|
|
popupShow.value = false;
|