getDates() {
const today = new Date();
const dayOfWeek = ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"];
const todayDate = today.toDateString(); // 获取今天的日期
const todayWeekDay = dayOfWeek[today.getDay()]; // 获取今天是周几
const futureDates = [{
date: this.convertDateToChinese(todayDate,'month'),
weekDay: todayWeekDay,
textname: this.convertDateToChinese(todayDate,'textname'),
check:true,
}];
for (let i = 1; i <= 3; i++) {
const futureDate = new Date(today.getTime() + i * 24 * 60 * 60 * 1000); // 当前时间加上i天
futureDates.push({
date: this.convertDateToChinese(futureDate.toDateString(),'month'),
weekDay: dayOfWeek[futureDate.getDay()],
textname: this.convertDateToChinese(futureDate.toDateString(),'textname'),
check:false,
});
}
// console.log(`今天的日期是: ${todayDate} (${todayWeekDay})`);
console.log(futureDates,'所有的')
this.list = futureDates;
this.checkTime = futureDates[0];
},
convertDateToChinese(dateStr,riqitype) {
const months = {
Jan: '01',
Feb: '02',
Mar: '03',
Apr: '04',
May: '05',
Jun: '06',
Jul: '07',
Aug: '08',
Sep: '09',
Oct: '10',
Nov: '11',
Dec: '12'
};
const dateParts = dateStr.split(' ');
const weekday = this.convertWeekdayToChinese(dateParts[0]);
const month = months[dateParts[1]];
const day = dateParts[2];
const year = dateParts[3];
if(riqitype == 'year'){
return `${year}-${month}-${day}`;
}else if(riqitype == 'month'){
return `${month}-${day}`;
}else if(riqitype == 'day'){
return `${day}`;
}else if(riqitype == 'weekday'){
return `${weekday}`;
}else if(riqitype == 'textname'){
return this.getDayName(`${year}-${month}-${day}`)
}
// return `${weekday} ${month} ${day} ${year}`;
},
}
})
},
convertWeekdayToChinese(weekday) {
const days = {
Sun: '星期日',
Mon: '星期一',
Tue: '星期二',
Wed: '星期三',
Thu: '星期四',
Fri: '星期五',
Sat: '星期六'
};
return days[weekday] || weekday;
},
getDayName(d){
d = d.replace(/-/g,'/')
var td=new Date();
td=new Date(td.getFullYear(),td.getMonth(),td.getDate());
var od=new Date(d);
od=new Date(od.getFullYear(),od.getMonth(),od.getDate());
var xc=(od-td)/1000/60/60/24;
if(xc<-2){
return -xc+"天前";
}else if(xc<-1){
return "前天";
}else if(xc<0){
return "昨天";
}else if(xc==0){
return "今天";
}else if(xc<2){
return "明天";
}else if(xc<3){
return "后天";
}else{
return xc+"天后";
}
},
获取当天以及未来三天的日期和周几
最新推荐文章于 2024-05-31 10:26:40 发布