python

def getTime(self,timeType):
        startdate = time.strftime("%Y-%m-%d", time.localtime())  // 获取格式为yyyy-MM-DD格式的当前日期 
        enddate =''
        if(timeType.strip()!='' and timeType=="day"):
            enddate = startdate
        elif(timeType.strip()!='' and timeType=="week"):
            enddate = (date.today() - timedelta(6)).strftime("%Y-%m-%d")      // 上一周日期
        elif(timeType!='' and timeType=="month"):
            today = datetime.today()
            if(today.month ==1):
                monthRange = calendar.monthrange(today.year-1,12)[1] // monthRange 是一个元组,获取上个月的天数
            else:
                monthRange = calendar.monthrange(today.year,today.month)[1]  // 获取上个月的天数
            print(monthRange)
            enddate = (date.today() - timedelta(monthRange-1)).strftime("%Y-%m-%d") // 获取上个月日期
            print(enddate)
        return {'startdate':enddate,'enddate':startdate}

js

computeTime(time, param2) {
      // startdate 是标准时间  Fri Dec 25 2020 08:00:00 GMT+0800 (中国标准时间)
      var startdate = new Date(this.startValue)
      var enddate = new Date(this.endValue)

      if (time === 'week') {
        if (param2 === 'last') {
          this.startValue = new Date(startdate - 7 * 24 * 60 * 60 * 1000)  //获取上周日期
          this.endValue = new Date(enddate - 7 * 24 * 60 * 60 * 1000)   //获取上周日期
        } else {
          this.startValue = new Date(startdate.getTime() + 7 * 24 * 60 * 60 * 1000)  
          this.endValue = new Date(enddate.getTime() + 7 * 24 * 60 * 60 * 1000)
        }
         this.startValue = this.formateDate(this.startValue)  //formateDate 将标准时间转换成yyyy-MM-DD格式的日期  
         this.endValue = this.formateDate(this.endValue)
      } else if (time === 'month') {
        // 获取开始日期的上个月有多少天
        if (param2 === 'last') {
          this.startValue = this.getLastMonthdays(startdate)  // 获取上个月的日期
          this.endValue = this.getLastMonthdays(enddate)
        } else {
          this.startValue = this.getNextMonthdays(startdate)   // 获取下个月的日期
          this.endValue = this.getNextMonthdays(enddate)
        }
      }
    },
    
     formateDate(standardDate) {
      var year = standardDate.getFullYear()
      var mm = standardDate.getMonth() + 1
      var dd = standardDate.getDate()
      if (mm < 10) {
        mm = '0' + mm
      }
      if (dd < 10) {
        dd = '0' + dd
      }
      return year + '-' + mm + '-' + dd
    },
    
     getLastMonthdays(standardDate) {
      var date = this.formateDate(standardDate)
      var arr = date.split('-')
      var year = arr[0]
      var month = arr[1]
      var day = arr[2]
      //  var days = new Date(year, month, 0)
      //  days = days.getDate()
      var year2 = year
      var month2 = parseInt(month) - 1
      if (month2 === 0) {
        year2 = parseInt(year2) - 1
        month2 = 12
      }
      var day2 = day
      var days2 = new Date(year2, month2, 0)
      days2 = days2.getDate()
      if (day2 > days2) {
        day2 = days2
      }
      if (month2 < 10) {
        month2 = '0' + month2
      }
      var t2 = year2 + '-' + month2 + '-' + day2
      return t2
    },
    getNextMonthdays(standardDate) {
      var date = this.formateDate(standardDate)
      var arr = date.split('-')
      var year = arr[0]
      var month = arr[1]
      var day = arr[2]
      //  var days = new Date(year, month, 0);
      //  days = days.getDate() //获取当前日期中的月的天数
      var year2 = year
      var month2 = parseInt(month) + 1
      if (month2 === 13) {
        year2 = parseInt(year2) + 1
        month2 = 1
      }
      var day2 = day
      var days2 = new Date(year2, month2, 0)
      days2 = days2.getDate()
      if (day2 > days2) {
        day2 = days2
      }
      if (month2 < 10) {
        month2 = '0' + month2
      }

      var t2 = year2 + '-' + month2 + '-' + day2
      return t2
    },
Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐