uniapp日期选择器

日期

在picker中加fields="month"就只能选择年月了,加fields="year"则只能选择年,默认是年月日格式

直接上代码,可以直接粘贴复制复用

<picker 
    mode="date" 
    :value="date" 
    fields="month" 
    :start="startDate" 
    :end="endDate"     
    @change="bindDateChange">
		<view>{{ date }}</view>
</picker>
<script>
function getDate(type) {
	const date = new Date();
	let year = date.getFullYear();
	let month = date.getMonth() + 1;
	let day = date.getDate();
	if (type === 'start') {
		year = year - 10;
	} else if (type === 'end') {
		year = year + 10;
	}
	month = month > 9 ? month : '0' + month;
	return `${year}-${month}`;
}
export default {
	data() {
		return {
			date: getDate({
				format: true
			}),
			startDate: getDate('start'),
			endDate: getDate('end'),
		};
	},
	methods: {
		bindDateChange(e) {
			this.date = e.detail.value.substr(0, 7);
			this.time = this.date;
			this.page = 1;
			this.list = [];
			this.index();
		},
	}
};
</script>

效果图

如果想要也显示每天的话把标签里的 fields="month" 删除即可,然后再在 getDate 里改变最后返回值即可 return `${year}-${month}-${day}`

评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值