fix: 日期选择问题修复。

This commit is contained in:
tianyongbao
2026-04-02 22:56:42 +08:00
parent eed1e5c994
commit 59709f1821

View File

@@ -55,7 +55,7 @@
</view>
<!-- 开始时间选择器 -->
<nut-popup v-model:visible="showStartPicker" position="bottom">
<nut-popup v-model:visible="showStartPicker" position="bottom" :style="{ paddingTop: '30px' }">
<nut-date-picker
v-model="startDateTime"
type="datetime"
@@ -64,11 +64,12 @@
:max-date="maxDate"
@confirm="confirmStartDate"
@cancel="showStartPicker = false"
ok-text="确定"
/>
</nut-popup>
<!-- 结束时间选择器 -->
<nut-popup v-model:visible="showEndPicker" position="bottom">
<nut-popup v-model:visible="showEndPicker" position="bottom" :style="{ paddingTop: '30px' }">
<nut-date-picker
v-model="endDateTime"
type="datetime"
@@ -77,6 +78,7 @@
:max-date="maxDate"
@confirm="confirmEndDate"
@cancel="showEndPicker = false"
ok-text="确定"
/>
</nut-popup>
</view>
@@ -209,17 +211,42 @@ function goBack() {
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 }) {
startDateTime.value = selectedValue[0];
startDate.value = formatDateMin(selectedValue[0]);
function confirmStartDate({ selectedValue }: { selectedValue: unknown }) {
const nextDate = buildDateFromPickerValue(selectedValue, startDateTime.value);
startDateTime.value = nextDate;
startDate.value = formatDateMin(nextDate);
showStartPicker.value = false;
}
// 确认结束时间
function confirmEndDate({ selectedValue }) {
endDateTime.value = selectedValue[0];
endDate.value = formatDateMin(selectedValue[0]);
function confirmEndDate({ selectedValue }: { selectedValue: unknown }) {
const nextDate = buildDateFromPickerValue(selectedValue, endDateTime.value);
endDateTime.value = nextDate;
endDate.value = formatDateMin(nextDate);
showEndPicker.value = false;
}
@@ -236,10 +263,10 @@ function queryData() {
}
// 验证时间范围
const start = new Date(startDate.value);
const end = new Date(endDate.value);
const start = startDateTime.value;
const end = endDateTime.value;
if (start >= end) {
if (start.getTime() >= end.getTime()) {
Taro.showToast({ title: '开始时间必须小于结束时间', icon: 'none' });
return;
}
@@ -728,4 +755,9 @@ function touchMove(e: any) {
font-size: 14px;
color: #999;
}
/* 日期选择器整体向下移动 - 避免与小程序关闭按钮重叠 */
:deep(.nut-picker) {
margin-top: 30px;
}
</style>