需求
- 1、默认当前时间
- 2、选择时间弹窗限制最大值、最小值
- 3、每次弹起更新最大值为当前时间,默认值为上次选中时间
- 4、==
minDate: new Date(2023, 10, 1).getTime()
,也可以传入时间字符串new Date('2023-10-1 12:22').getTime()
==
html
<view class="flex bb ptb-12">
<view><text class="red">* </text>处理时间:</view>
<view class="flex1 size-28" bindtap="chooseTime">
<view class="mr-8">{{ququ2?ququ2:'请选择'}}</view>
<van-icon name="arrow" />
</view>
</view>
<!-- 弹窗 -->
<van-popup show="{{ isShowPop }}" bind:close="onClosePop" position="bottom">
<van-datetime-picker title="处理时间" formatter="{{formatter}}" value="{{ currentDate }}" bind:confirm="confirmPop" bind:cancel="onClosePop" min-date="{{minDate}}" max-date="{{maxDate}}" />
</van-popup>
data
ququ2: '',
isShowPop: false,
currentDate: new Date().getTime(),
minDate: new Date(2023, 10, 1).getTime(),
maxDate: new Date().getTime(),
formatter: function (type, value) {
if (type === 'year') {
return `${value}年`;
} else if (type === 'month') {
return `${value}月`;
} else if (type === 'day') {
return `${value}日`;
} else if (type === 'hour') {
return `${value}时`;
} else if (type === 'minute') {
return `${value}分`;
}
return value;
},
方法
chooseTime() {
this.setData({
maxDate: new Date().getTime(),
currentDate: this.data.ququ2 ? new Date(this.data.ququ2).getTime() : new Date().getTime(),
isShowPop: true
})
},
confirmPop(e) {
this.setData({
ququ2: this.formatTimestamp(e.detail),
isShowPop: false
})
},
onClosePop() {
this.setData({
isShowPop: false
})
},
formatTimestamp(timestamp) {
var date = new Date(timestamp);
var year = date.getFullYear();
var month = date.getMonth() + 1;
var day = date.getDate();
var hour = date.getHours();
var minute = date.getMinutes();
month = month < 10 ? '0' + month : month;
day = day < 10 ? '0' + day : day;
hour = hour < 10 ? '0' + hour : hour;
minute = minute < 10 ? '0' + minute : minute;
return `${year}-${month}-${day} ${hour}:${minute}`;
},