uniapp uview 通过 “时间戳” 实现日历 点击上一月,下一月的功能组件

借鉴链接: js 写点击上一月 下一月时候切换.
实现:在这里插入图片描述

html:引用的是uview的日期控件

<template>
	<view class="chooise-data">
		<view class="yesterday" @click="prevDate()">
			上一月
		</view>
		<view class="chooise-box">
			<view class="date">
				<view class="calendar">
					<u-calendar v-model="show" ref="calendar" @change="change" :mode="mode" :btn-type="btnType"></u-calendar>
					<view @click="showChange()">{{result}}</view>
				</view>
				<view class="date-icon">
					<image src="../static/img/home/pop-date.png" mode=""></image>
				</view>
			</view>
		</view>
		<view class="tomorrow" @click="nextDate">
			下一月
		</view>
	</view>
</template>

javascript 部分

<script>
	export default {
		data() {
			return {
				date: '',
				show: false,
				mode: 'date',
				result: this.result,
				btnType: 'primary',
			};
		},
		beforeMount() {
			this.result = this.getcurrentday()
		},

		methods: {

			// 获取当前月期展示
			getcurrentday() {
				var date = new Date();
				let year = date.getFullYear();
				let month = date.getMonth() + 1;
				let day = date.getDate();
				month = month < 10 ? "0" + month : month; //月小于10,加0
				day = day < 10 ? "0" + day : day; //day小于10,加0
				return `${year}-${month}`;
			},

			// 切换上一月
			prevDate() {
				let date = new Date(this.result); //获取当前的日期
				let CurrentDate = ""; 
				let year = date.getFullYear();
				let month = (date.getMonth()) < 10 ? "0" + (date.getMonth()) : (date.getMonth()); //自动补0
				//判断月份是否在当前年份
				if (month < 1) {
					CurrentDate = (year - 1) + "-" + "12"
				} else {
					CurrentDate = year + "-" + month
				}
				this.result = CurrentDate;
			},
			
			// 切换下一月
			nextDate() {
				let date = new Date(this.result);
				let CurrentDate = "";
				let year = date.getFullYear();
				let month = (date.getMonth() + 1 + 1) < 10 ? "0" + (date.getMonth() + 1 + 1) : (date.getMonth() + 1 + 1);
				//判断月份是否在当前年份
				if (month > 12) {
					CurrentDate = (year + 1) + "-" + "01"
				} else {
					CurrentDate = year + "-" + month
				}
				this.result = CurrentDate;
			},

			showChange(index) {
				this.show = true; //是否显示控件
			},

			change(e) {
				console.log(e)
				let year = e.year;
				let month = e.month;
				month = month < 10 ? "0" + month : month;
				this.result = `${year}-${month}`; //当前月期显示到页面组件
			}
		},
		watch: {
			chooisedate(val) {
				this.$emit('result', this.result) //子组件传值
			}
		},
	}
</script>
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值