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

this.checkAuditTime('09:00','18: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
          }
    },

原文链接
https://www.bigtspace.com/archives/10919.html

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容