關(guān)于vant的時(shí)間選擇器使用方式
更新時(shí)間:2024年03月22日 09:30:55 作者:Emotion#
這篇文章主要介紹了關(guān)于vant的時(shí)間選擇器使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
vant的時(shí)間選擇器
<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>
這里需要開始時(shí)間和結(jié)束時(shí)間:
- 提示:因此增加了showTwoTime的判定:
解決方案
- 提示:這里是時(shí)間轉(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'為了提示自己時(shí)間格式
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) //從時(shí)間選擇器中得到的時(shí)間格式為時(shí)間搓,因此需要轉(zhuǎn)換為標(biāo)準(zhǔn)制式時(shí)間單位
console.log(date)
var year = date.getFullYear()
var month = date.getMonth() + 1
var day = date.getDate() //這里只表現(xiàn)到日,時(shí),分,秒自習(xí)行添加方法!
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
},
展示效果


總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
VUE項(xiàng)目中加載已保存的筆記實(shí)例方法
在本篇文章里小編給大家整理了一篇關(guān)于VUE項(xiàng)目中加載已保存的筆記實(shí)例方法,有興趣的讀者們可以參考下。2019-09-09
Vue生態(tài)系統(tǒng)工具庫(kù)Vueuse的使用示例詳解
Vueuse 是一個(gè)功能強(qiáng)大的 Vue.js 生態(tài)系統(tǒng)工具庫(kù),它提供了一系列的可重用的 Vue 組件和函數(shù),本文將介紹 Vueuse 的主要特點(diǎn)和用法,以及它在 Vue.js 開發(fā)中的作用和優(yōu)勢(shì),感興趣的可以了解下2024-02-02
Vue實(shí)現(xiàn)天氣預(yù)報(bào)小應(yīng)用
這篇文章主要為大家詳細(xì)介紹了Vue實(shí)現(xiàn)天氣預(yù)報(bào)小應(yīng)用,查詢一些城市的天氣情況,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08
vue.js數(shù)據(jù)綁定的方法(單向、雙向和一次性綁定)
本篇文章主要介紹了vue.js數(shù)據(jù)綁定的方法(單向、雙向和一次性綁定),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07
Vue入門之?dāng)?shù)量加減運(yùn)算操作示例
這篇文章主要介紹了Vue入門之?dāng)?shù)量加減運(yùn)算操作,結(jié)合實(shí)例形式分析了vue.js基本數(shù)值運(yùn)算相關(guān)操作技巧,需要的朋友可以參考下2018-12-12
Vue結(jié)合SignalR實(shí)現(xiàn)前后端實(shí)時(shí)消息同步
這篇文章主要為大家詳細(xì)介紹了Vue結(jié)合SignalR實(shí)現(xiàn)前后端實(shí)時(shí)消息同步,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-09-09

