關于vant的時間選擇器使用方式
更新時間:2024年03月22日 09:30:55 作者:Emotion#
這篇文章主要介紹了關于vant的時間選擇器使用方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
vant的時間選擇器
<van-popup :show="showPop" position="bottom" label="有效日期" custom-style="height: 50%;" @close="onCancel" > <view v-if="showTwoTime"> <van-datetime-picker type="date" :value="currentDate" @confirm="confirm1" @cancel="onCancel" :min-date="minDate" :formatter="formatter" /> </view> <view v-if="!showTwoTime"> <van-datetime-picker type="date" :value="currentDate" @confirm="confirm2" @cancel="onCancel" :min-date="minDate" :formatter="formatter" /> </view ></van-popup>
這里需要開始時間和結束時間:
- 提示:因此增加了showTwoTime的判定:
解決方案
- 提示:這里是時間轉(zhuǎn)換的方法:
confirm1(value) { this.plan.start_time = this.formatTime(value.detail, 'Y/M/D') this.showTwoTime = false }, confirm2(value) { this.showPop = false this.plan.end_time = this.formatTime(value.detail, 'Y/M/D') this.showTwoTime = true }, formatTime(date) { date = new Date(date) console.log(date) var year = date.getFullYear() var month = date.getMonth() + 1 var day = date.getDate() return [year, month, day].map(this.formatNumber).join('/') }, formatNumber(n) { n = n.toString() return n[1] ? n : '0' + n },
解決方案
- 提示:全部方法:
<van-popup :show="showPop" position="bottom" label="有效日期" custom-style="height: 50%;" @close="onCancel" > <view v-if="showTwoTime"> <van-datetime-picker type="date" :value="currentDate" @confirm="confirm1" @cancel="onCancel" :min-date="minDate" :formatter="formatter" /> </view> <view v-if="!showTwoTime"> <van-datetime-picker type="date" :value="currentDate" @confirm="confirm2" @cancel="onCancel" :min-date="minDate" :formatter="formatter" /> </view ></van-popup>
//data的定義 showPop: false, currentDate: new Date().getTime(), minDate: new Date().getTime(), showTwoTime: true,
//方法的定義 changeFn() { this.changeDate = this.currentDate }, confirm1(value) { this.plan.start_time = this.formatTime(value.detail, 'Y/M/D') ///'Y/M/D'為了提示自己時間格式 this.showTwoTime = false }, confirm2(value) { this.showPop = false this.plan.end_time = this.formatTime(value.detail, 'Y/M/D') this.showTwoTime = true }, formatTime(date) { date = new Date(date) //從時間選擇器中得到的時間格式為時間搓,因此需要轉(zhuǎn)換為標準制式時間單位 console.log(date) var year = date.getFullYear() var month = date.getMonth() + 1 var day = date.getDate() //這里只表現(xiàn)到日,時,分,秒自習行添加方法! return [year, month, day].map(this.formatNumber).join('/') //轉(zhuǎn)換為產(chǎn)品經(jīng)理想要的展示形式 }, formatNumber(n) { n = n.toString() return n[1] ? n : '0' + n //加0操作! }, formatter(type, value) { //展示的格式處理 if (type === 'year') { return `${value}年` } if (type === 'month') { return `${value}月` } if (type === 'day') { return `${value}日` } return value },
展示效果
總結
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Vue生態(tài)系統(tǒng)工具庫Vueuse的使用示例詳解
Vueuse 是一個功能強大的 Vue.js 生態(tài)系統(tǒng)工具庫,它提供了一系列的可重用的 Vue 組件和函數(shù),本文將介紹 Vueuse 的主要特點和用法,以及它在 Vue.js 開發(fā)中的作用和優(yōu)勢,感興趣的可以了解下2024-02-02vue.js數(shù)據(jù)綁定的方法(單向、雙向和一次性綁定)
本篇文章主要介紹了vue.js數(shù)據(jù)綁定的方法(單向、雙向和一次性綁定),具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-07-07