uniapp日期时间
文章描述:
uniapp年月日选择
微信开发者工具效果
安卓手机效果
<template>
<view class="container">
<picker mode="date" :value="date" :start="startDate" :end="endDate" fields="day" @change="bindDateChange">
<view class="uni-input">{{date}}</view>
</picker>
<button @click="submit">按钮</button>
</view>
</template>
javascript
export default {
data() {
const now = new Date();
const currentDate = this.getDate({
format: true
})
return {
date: currentDate
}
},
computed:{
startDate() {
return this.getDate('start');
},
endDate() {
return this.getDate('end');
}
},
methods:{
submit(){
console.log(this.date)
},
bindDateChange: function(e) { //选择日期
this.date = e.detail.value
console.log('date', this.date)
},
getDate(type) { //年月日
const date = new Date();
// const date = new Date();
let year = date.getFullYear();
let month = date.getMonth() + 1;
let day = date.getDate();
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}-${day}`;
},
}
}
结果:2024-12-16
发布时间:2024/12/16
发表评论