判断天的函数传入的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);
},