欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

开发小工具

程序员文章站 2022-06-24 17:01:23
...

判断给定两个日期之间的间隔天数,是否超过某短时间

/**
       *  判断两个时间之间是否超过给定天数
       *  @param: date1 { string|date }, yyyy-MM-dd 要判断的时间
       *  @param: date2 { string|date }, yyyy-MM-dd 要判断的时间
       *  @param: limit { number }, 给定的天数
       * */
      isOutPass (firstDate, secondDate, limit) {
        if (!firstDate || !secondDate || !limit) {
          return false
        }
        const firstDateTimestamp = new Date(firstDate.replace(/-/g, '/')).getTime()
        const secondDateTimestamp = new Date(secondDate.replace(/-/g, '/')).getTime()
        const limitTimestamp = limit * 24 * 60 * 60 * 1000
        if (Math.abs(firstDateTimestamp - secondDateTimestamp) > limitTimestamp) {
          return false
        }
        return true
      }

获取当前日期和30天前的日期

getDefaultData () {
        const date1 = new Date()
        const date2 = new Date(date1)
        // -30为30天前,+30可以获得30天后的日期
        date2.setDate(date1.getDate() - 30)
        // 30天前(月份判断是否小于10,小于10的前面+0)
        const agoDay = `${date2.getFullYear()}-${date2.getMonth() + 1 < 10 ? `0${date2.getMonth() + 1}` : date2.getMonth() + 1}-${date2.getDate()}`
        // 当前日期
        const nowDay = `${date1.getFullYear()}-${date1.getMonth() + 1 < 10 ? `0${date1.getMonth() + 1}` : date1.getMonth() + 1}-${date1.getDate()}`
        return [agoDay, nowDay]
      },