fix: 日期选择问题修复。
This commit is contained in:
@@ -55,7 +55,7 @@
|
|||||||
</view>
|
</view>
|
||||||
|
|
||||||
<!-- 开始时间选择器 -->
|
<!-- 开始时间选择器 -->
|
||||||
<nut-popup v-model:visible="showStartPicker" position="bottom">
|
<nut-popup v-model:visible="showStartPicker" position="bottom" :style="{ paddingTop: '30px' }">
|
||||||
<nut-date-picker
|
<nut-date-picker
|
||||||
v-model="startDateTime"
|
v-model="startDateTime"
|
||||||
type="datetime"
|
type="datetime"
|
||||||
@@ -64,11 +64,12 @@
|
|||||||
:max-date="maxDate"
|
:max-date="maxDate"
|
||||||
@confirm="confirmStartDate"
|
@confirm="confirmStartDate"
|
||||||
@cancel="showStartPicker = false"
|
@cancel="showStartPicker = false"
|
||||||
|
ok-text="确定"
|
||||||
/>
|
/>
|
||||||
</nut-popup>
|
</nut-popup>
|
||||||
|
|
||||||
<!-- 结束时间选择器 -->
|
<!-- 结束时间选择器 -->
|
||||||
<nut-popup v-model:visible="showEndPicker" position="bottom">
|
<nut-popup v-model:visible="showEndPicker" position="bottom" :style="{ paddingTop: '30px' }">
|
||||||
<nut-date-picker
|
<nut-date-picker
|
||||||
v-model="endDateTime"
|
v-model="endDateTime"
|
||||||
type="datetime"
|
type="datetime"
|
||||||
@@ -77,6 +78,7 @@
|
|||||||
:max-date="maxDate"
|
:max-date="maxDate"
|
||||||
@confirm="confirmEndDate"
|
@confirm="confirmEndDate"
|
||||||
@cancel="showEndPicker = false"
|
@cancel="showEndPicker = false"
|
||||||
|
ok-text="确定"
|
||||||
/>
|
/>
|
||||||
</nut-popup>
|
</nut-popup>
|
||||||
</view>
|
</view>
|
||||||
@@ -209,17 +211,42 @@ function goBack() {
|
|||||||
Taro.navigateBack();
|
Taro.navigateBack();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function buildDateFromPickerValue(value: unknown, fallback: Date) {
|
||||||
|
if (value instanceof Date && !Number.isNaN(value.getTime())) {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (typeof value === 'number' || typeof value === 'string') {
|
||||||
|
const date = new Date(value);
|
||||||
|
if (!Number.isNaN(date.getTime())) {
|
||||||
|
return date;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Array.isArray(value) && value.length >= 5) {
|
||||||
|
const [year, month, day, hour, minute] = value.map((item) => Number(item));
|
||||||
|
const date = new Date(year, month - 1, day, hour, minute, 0);
|
||||||
|
if (!Number.isNaN(date.getTime())) {
|
||||||
|
return date;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return fallback;
|
||||||
|
}
|
||||||
|
|
||||||
// 确认开始时间
|
// 确认开始时间
|
||||||
function confirmStartDate({ selectedValue }) {
|
function confirmStartDate({ selectedValue }: { selectedValue: unknown }) {
|
||||||
startDateTime.value = selectedValue[0];
|
const nextDate = buildDateFromPickerValue(selectedValue, startDateTime.value);
|
||||||
startDate.value = formatDateMin(selectedValue[0]);
|
startDateTime.value = nextDate;
|
||||||
|
startDate.value = formatDateMin(nextDate);
|
||||||
showStartPicker.value = false;
|
showStartPicker.value = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 确认结束时间
|
// 确认结束时间
|
||||||
function confirmEndDate({ selectedValue }) {
|
function confirmEndDate({ selectedValue }: { selectedValue: unknown }) {
|
||||||
endDateTime.value = selectedValue[0];
|
const nextDate = buildDateFromPickerValue(selectedValue, endDateTime.value);
|
||||||
endDate.value = formatDateMin(selectedValue[0]);
|
endDateTime.value = nextDate;
|
||||||
|
endDate.value = formatDateMin(nextDate);
|
||||||
showEndPicker.value = false;
|
showEndPicker.value = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -236,10 +263,10 @@ function queryData() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 验证时间范围
|
// 验证时间范围
|
||||||
const start = new Date(startDate.value);
|
const start = startDateTime.value;
|
||||||
const end = new Date(endDate.value);
|
const end = endDateTime.value;
|
||||||
|
|
||||||
if (start >= end) {
|
if (start.getTime() >= end.getTime()) {
|
||||||
Taro.showToast({ title: '开始时间必须小于结束时间', icon: 'none' });
|
Taro.showToast({ title: '开始时间必须小于结束时间', icon: 'none' });
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -728,4 +755,9 @@ function touchMove(e: any) {
|
|||||||
font-size: 14px;
|
font-size: 14px;
|
||||||
color: #999;
|
color: #999;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* 日期选择器整体向下移动 - 避免与小程序关闭按钮重叠 */
|
||||||
|
:deep(.nut-picker) {
|
||||||
|
margin-top: 30px;
|
||||||
|
}
|
||||||
</style>
|
</style>
|
||||||
|
|||||||
Reference in New Issue
Block a user