获取当天以及未来三天的日期和周几

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+"天后";
	   }
	},
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值