關(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è)參考,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- Vue使用vant實(shí)現(xiàn)日期選擇器功能
- vant4 封裝日期段選擇器的實(shí)現(xiàn)
- Vue配合Vant使用時(shí)area省市區(qū)選擇器的使用方式
- Vant picker選擇器設(shè)置默認(rèn)值導(dǎo)致選擇器失效的解決
- Vant?Weapp組件picker選擇器初始默認(rèn)選中問題
- vant中的picker選擇器自定義選項(xiàng)內(nèi)容
- 基于Vant UI框架實(shí)現(xiàn)時(shí)間段選擇器
- Vue 用Vant實(shí)現(xiàn)時(shí)間選擇器的示例代碼
- vant實(shí)現(xiàn)自定義日期時(shí)間選擇器(年月日時(shí)分秒)
相關(guān)文章
Vue 將后臺(tái)傳過來的帶html字段的字符串轉(zhuǎn)換為 HTML
這篇文章主要介紹了Vue 將后臺(tái)傳過來的帶html字段的字符串轉(zhuǎn)換為 HTML ,需要的朋友可以參考下2018-03-03
解決vue?app.js/vender.js過大優(yōu)化啟動(dòng)頁(yè)
這篇文章主要為大家介紹了解決vue?app.js/vender.js過大優(yōu)化啟動(dòng)頁(yè)過程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10
策略模式實(shí)現(xiàn) Vue 動(dòng)態(tài)表單驗(yàn)證的方法
策略模式(Strategy Pattern)又稱政策模式,其定義一系列的算法,把它們一個(gè)個(gè)封裝起來,并且使它們可以互相替換。封裝的策略算法一般是獨(dú)立的,策略模式根據(jù)輸入來調(diào)整采用哪個(gè)算法。這篇文章主要介紹了策略模式實(shí)現(xiàn) Vue 動(dòng)態(tài)表單驗(yàn)證,需要的朋友可以參考下2019-09-09
Vue 路由切換時(shí)頁(yè)面內(nèi)容沒有重新加載的解決方法
今天小編就為大家分享一篇Vue 路由切換時(shí)頁(yè)面內(nèi)容沒有重新加載的解決方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-09-09
vue自定義權(quán)限標(biāo)簽詳細(xì)代碼示例
這篇文章主要給大家介紹了關(guān)于vue自定義權(quán)限標(biāo)簽的相關(guān)資料,在Vue中你可以通過創(chuàng)建自定義組件來實(shí)現(xiàn)自定義標(biāo)簽組件,文中給出了詳細(xì)的代碼示例,需要的朋友可以參考下2023-09-09

