怎么算当前的时间和计划的实施时间进行对比,是否超时?

112 阅读1分钟

怎么算当前的时间和计划的实施时间进行对比,是否超时? private timeFunction(timesTart: string, timeEnd: string) { //timesTart 形参开始时间 //timeEnd 形参结束时间 //传入的结束时间要大于传入的开始时间,避免出现负数 //Math.round()计算四舍五入的方法,原理:【给()里面的数据+0.5】注意:【Math.round(-1.5)的结果:-1】所以该方法尽量不要传入负数 const mistiming = Math.round((new Date(timeEnd).getTime() - new Date(timesTart).getTime()) / 1000);//获取计算后的相差的时间(秒) const [FORMAT, FORMATSIZE] = [ ["年", "个月", "星期", "天", "时", "分"], [31536000, 2592000, 604800, 86400, 3600, 60], ]; //建立两个数组,改方法等同于:{ const FORMAT= ["年", "个月", "星期", "天", "时", "分"]; const FORMATSIZE= [31536000, 2592000, 604800, 86400, 3600, 60]; } //--------- 核心代码 ------------ //循环FORMAT,以为 FORMAT,FORMATSIZE 俩个数组的元素一一对应所以可以共用数组下标[i] for (let i = 0; i < FORMAT.length; i++) { const inms = Math.floor(mistiming / FORMATSIZE[i]);//计算向下取整方法, if (inms !== 0) { // Math.floor()方法结果不为0判断,原理依据:【例:1/2=0.5,1/3=0.3....,2/1=2,3/2=1.5,(较小的数除上较大的数一定会得到一个小于1的数,较大的数除上较小的数一定会得到一个大于1的数),向下取整的结果只会存在‘=0’,‘>1’两种结果】 return inms + FORMAT[i];//符合判断返回数值+单位,结束方法 } } //------------------- }

  <template #column-endLimitTime="{ row }">
    <div v-if="new Date(row.planEndTime) < new Date()" style="color: #e20000"
      ><el-tag type="danger" size="mini" style="margin-right: 6px">超期</el-tag>{{ timeFunction(row.planEndTime, new Date()) }}</div
    >
    <span v-else style="color: #0262c9">{{ timeFunction(new Date(), row.planEndTime) }}</span>
  </template>