uniapp 通过左右按钮以天/周为单位选择时间

判断天的函数传入的date为时间戳

formatRelativeTime(date) {
				const now = new Date();
				const compareDate = new Date(date);

				const todayStart = new Date(now.getFullYear(), now.getMonth(), now.getDate());
				const yesterdayStart = new Date(todayStart.getTime() - 24 * 60 * 60 * 1000);

				let relativeTime;
				if (compareDate >= todayStart && compareDate <= now) {
					relativeTime = '今天';
				} else if (compareDate >= yesterdayStart && compareDate < todayStart) {
					relativeTime = '昨天';
				} else {
					return this.date.getFullYear() + '年' + ((this.date.getMonth() * 1) + 1) + '月' + this.date.getDate() +
						'日'

				}
				return relativeTime;
			},

判断周的函数传入的date为时间戳

setWeekTime(timestamp) {
				const now = new Date();
				const nowStartOfWeek = new Date(now.getFullYear(), now.getMonth(), now.getDate() - now.getDay() + 1, 0, 0,
					0);
				const nowEndOfWeek = new Date(nowStartOfWeek);
				nowEndOfWeek.setDate(nowEndOfWeek.getDate() + 6);
				nowEndOfWeek.setHours(23, 59, 59, 999);
				const dateToCheck = new Date(timestamp);
				if (dateToCheck >= nowStartOfWeek && dateToCheck <= nowEndOfWeek) {
					return '本周'
				}
				// 将时间戳转换为Date对象
				var date = new Date(timestamp); // 如果时间戳是秒数,否则无需乘以1000
				// 获取当前日期是一周中的哪一天(0表示周日,1表示周一,...,6表示周六)
				var currentDayOfWeek = date.getDay();
				// 计算本周开始日期(这里设定周一为一周的开始)
				var startOfWeek = new Date(date);
				startOfWeek.setDate(date.getDate() - currentDayOfWeek + (currentDayOfWeek === 0 ? -6 :
					1)); // 如果当天是周日,则向前推两天到上周的周一
				startOfWeek.setHours(0, 0, 0, 0); // 清除时间部分
				const endOfWeek = new Date(startOfWeek);
				endOfWeek.setDate(endOfWeek.getDate() + 6);
				endOfWeek.setHours(23, 59, 59, 999);
				return this.convertDate(startOfWeek.toDateString()) + '至' + this.convertDate(endOfWeek)
			},

data数据的定义

// 当前的日期或周的显示
atNowDate: '',
date: new Date(),
// 切换时间单位的数据
selectTimeData: [{
	    title: '每天',
	    id: 0
    },
	{
		title: '每周',
		id: 1
	}
],
// 当前的单位的索引
atThisSelectTimeIndex: 0,

前进时间

dateAdvance() {
				console.log('时间前进');
				if (this.atNowDate == '今天' || this.atNowDate == '本周') return;
				if (this.atThisSelectTimeIndex == 0) {
					this.date = new Date(this.date);
					let a = this.date.setDate(this.date.getDate() + 1);
					let showDate = this.formatRelativeTime(a);
					this.atNowDate = showDate;
				} else {
					this.date = new Date(this.date);
					let a = this.date.setDate(this.date.getDate() + 7);
					let showDate = this.setWeekTime(a);
					this.atNowDate = showDate;
				}
			},

时间后退

dateBackward() {
				console.log('时间后退')
				if (this.atThisSelectTimeIndex == 0) {
					this.date = new Date(this.date);
					let a = this.date.setDate(this.date.getDate() - 1);
					let showDate = this.formatRelativeTime(a);
					this.atNowDate = showDate;
					console.log(a)

				} else {
					this.date = new Date(this.date);
					let a = this.date.setDate(this.date.getDate() - 7);
					let showDate = this.setWeekTime(a);
					this.atNowDate = showDate;
				}

			},

切换单位索引的函数

switchSelectTimeIndex(index) {
				this.atThisSelectTimeIndex = index;
				this.date = new Date()
				if (index == 0) {
					let showDate = this.formatRelativeTime(this.date);
					this.atNowDate = showDate;
					console.log('this is ', showDate)
				} else {
					let showWeek = this.setWeekTime(this.date.getTime());
					this.atNowDate = showWeek;
				}
			},

进入页面时的渲染

onShow() {
			this.switchSelectTimeIndex(0);
		},

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值