侧边栏壁纸
  • 累计撰写 2,046 篇文章
  • 累计创建 73 个标签
  • 累计收到 20 条评论

目 录CONTENT

文章目录

Vue判断当前时间是否在某个时间范围

大猿本猿
2023-04-07 / 1,241 阅读 / 227 字

Vue判断当前时间是否在某个时间范围

" "

需求描述

判断当前时间是否在7:00-8:00范围内,根据时间来区分业务限制。

解决方案

写要给检测函数

//检查当前时间是否在时间范围内
    checkAuditTime(startTime, endTime) {
      // 获取当前时间
      const date = new Date()
      // 获取当前时间的年月日
      const dataStr = `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()} `

      // 获取开始时间、结束时间、现在时间的时间戳
      let startDate = new Date(dataStr + startTime).getTime()
      let endDate = new Date(dataStr + endTime).getTime()
      let nowDate = date.getTime()

      const s = startDate > endDate // 判断开始时间否大于结束时间

      if (s) [startDate, endDate] = [endDate, startDate] // 若开始时间否大于结束时间则交换值

      // 判断现在的时间是否在开始时间和结束时间之间,若s为true则结果取反
      if (nowDate > startDate && nowDate < endDate) {
        return s ? false : true
      } else {
        return s ? true : false
      }
    },

使用

console.log(checkAuditTime('07:00', '08:00'));