關于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的判定:
解決方案
- 提示:這里是時間轉換的方法:
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) //從時間選擇器中得到的時間格式為時間搓,因此需要轉換為標準制式時間單位
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('/') //轉換為產品經理想要的展示形式
},
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
},
展示效果


總結
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Vue 將后臺傳過來的帶html字段的字符串轉換為 HTML
這篇文章主要介紹了Vue 將后臺傳過來的帶html字段的字符串轉換為 HTML ,需要的朋友可以參考下2018-03-03
解決vue?app.js/vender.js過大優(yōu)化啟動頁
這篇文章主要為大家介紹了解決vue?app.js/vender.js過大優(yōu)化啟動頁過程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-10-10
策略模式實現(xiàn) Vue 動態(tài)表單驗證的方法
策略模式(Strategy Pattern)又稱政策模式,其定義一系列的算法,把它們一個個封裝起來,并且使它們可以互相替換。封裝的策略算法一般是獨立的,策略模式根據輸入來調整采用哪個算法。這篇文章主要介紹了策略模式實現(xiàn) Vue 動態(tài)表單驗證,需要的朋友可以參考下2019-09-09

