获取本周、本月开始时间和结束时间

   formatDate(date) {
       const y = date.getFullYear()
       let m = date.getMonth() + 1
       m = m<10 ? ('0'+m) : m
       let d = date.getDate()
       d = d < 10? ('0'+d) : d
       return y + '-' + m + '-' + d
   }
   getDateStr(dayCount) {
      const dd = new Date()
      dd.setDate(dd.getDate() + dayCount) // 获取dayCount天后的日期
      const y = dd.getFullYear()
      const m = (dd.getMonth() + 1) < 10 ? '0' + (dd.getMonth() + 1) : (dd.getMonth() + 1)// 获取当前月份的日期,不足10补0
      const d = dd.getDate() < 10 ? '0' + dd.getDate() : dd.getDate() // 获取当前几号,不足10补0
      return y + '-' + m + '-' + d
  }
// 获取本周开始时间和结束时间(截止今天)
  getThisWeek() {
    const now = new Date() // 当前日期
    // 今天本周的第几天
    let nowDayOfWeek = now.getDay()
    // 0 代表星期日
    if (nowDayOfWeek === 0) {
      nowDayOfWeek = 7
    }
    const t1 = getDateStr(-(nowDayOfWeek - 1))
    const t2 = getDateStr(0)
    return [t1, t2]
  }

  // 获取本月开始时间和结束时间(截止今天)
  getThisMonth(){
    const now = new Date() // 当前日期
    
    const nowMonth = now.getMonth() // 当前月(0~11)
    const nowYear = now.getFullYear() // 当前年
    // 获取本月开始时间格式 new Date(年, 月(0~11), 1)
    const t1 = formatDate(new Date(nowYear, nowMonth, 1))
    const thisDay = now.getDate()
    // 获取本月结束时间格式 new Date(年, 月(0~11), 本月总天数)
    const t2 = formatDate(new Date(nowYear, nowMonth, thisDay))
    return [t1, t2]
  }

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值