uniapp如何使用picker寫一個只顯示年份和月份的下拉框
阿新 • • 發佈:2021-01-20
效果:
<picker mode="date" :value="date" fields="month" :start="startDate" :end="endDate" @change="dateChange">
<view>{{date}}</view>
</picker>
js:
export default {
data() {
// 設定日期
const currentDate = this.getDate({
format: 'yyyy-mm'
})
return {
date: currentDate,
}
},
computed: {
startDate() {
return this.getDate('start');
},
endDate() {
return this .getDate('end');
}
},
methods: {
// 選擇日期
dateChange(e) {
this.date = e.target.value.slice(0,7);
console.log(this.date.slice(0,7));
},
// 獲取年月日資訊
getDate(type) {
const date = new Date();
let year = date.getFullYear();
let month = date.getMonth() + 1;
if (type === 'start') {
year = year - 60;
} else if (type === 'end') {
year = year + 2;
}
month = month > 9 ? month : '0' + month;
// day = day > 9 ? day : '0' + day;
return `${year}-${month}`;
}
}
}
</script>
轉發請註明原創噢~~~~
看完記得點個贊喲!!!