判断时间(今天、明天、后天)展示
funtion computedTime(time:string){
const targetTime = new Date(time), curTime = new Date() // 创建目标时间对象
// 获取目标计算时间和当前时间,如果需要手动上传当前时间,多加一个时间参数并创建对应的时间对象或直接传入时间对象即可
const targetDay = targetTime.getDate(), curDay = curTime.getDate(), min = targetTime.getMinutes()
let diffDay = targetDay - curDay;// 天数差
let diffMonth = targetTime.getMonth() - curTime.getMonth() // 月数差
let diffYear = targetTime.getFullYear() - curTime.getFullYear() // 年数差
let flag = false; // 是否计算跨月/跨年
if (diffYear !== 0) {
// 不等于当年,即跨年
flag = true
} else {
// 当年
if (diffMonth !== 0) {
// 不等于当月,即跨月。当年不当月
flag = true
} else {
// 当年当月,不二次计算
}
}
if (flag) {
if (diffMonth === 1) {
// 只跨一个月
const dayNum = new Date(curTime.getFullYear(), curTime.getMonth() + 1, 0).getDate(); // 这个月的天数
const diffNum = dayNum - curDay // 当月天数差
diffDay = diffNum + targetTime.getDate() // 天数差
} else {
// 不当年并且差几个月
diffDay = 99 // 设置较大的天数差,直接展示月
}
}
switch (diffDay) {
case 0:
// 今天
case 1:
// 明天
case 2:
// 后天
default:
// 相隔天数大于2天
}
}