js 根据已知的时间,判断是否是今天,明天,昨

2025-05-20 04:33:07
推荐回答(1个)
回答1:

function getDayName(d){
   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+"天后";
   }
}
console.log(getDayName("2018-7-9 1:35:23")); //用法举例:昨天
console.log(getDayName("2018-7-7 21:45:19")); //用法举例:3天前
console.log(getDayName("2018-7-12 8:21:54")); //用法举例:后天