js中的日期Date操作——yyyy-mm-dd格式日期相差天数

countGap(start, end){
      let gap = "-";
      start = start.split(gap);
      end = end.split(gap);
      let strStart = new Date(start[0], start[1]-1, start[2]);
      let strEnd = new Date(end[0], end[1]-1, end[2]);
      // 相差的毫秒数转换为天数
      let gapDays = parseInt(Math.abs(strStart - strEnd ) / 1000 / 60 / 60 /24) 
      return gapDays ;
    },
getDaysDifference(dateToday, compareDate) {
            const oneDay = 24 * 60 * 60 * 1000; // 一天的毫秒数
            const firstDate = new Date(dateToday);
            const secondDate = new Date(compareDate);
            // 作比较的日期减去今天的日期
            const diffDays = Math.round((secondDate - firstDate) / oneDay); 
            return diffDays;
        },

你可能感兴趣的:(javascript,前端,开发语言)