【dayjs使用】取 本周、上周、本月、上月、本季度、上季度时间段

3 篇文章 0 订阅
3 篇文章 0 订阅
let dateTimes = [
        {
          id: 1,
          name: '本周',
          start_time: dayjs().startOf('week').add(1, 'day').format('YYYY-MM-DD'),
          end_time: dayjs().endOf('week').add(1, 'day').format('YYYY-MM-DD'),
        },
        {
          id: 2,
          name: '上周',
          start_time: dayjs().add(-1, 'week').startOf('week').add(1, 'day').format('YYYY-MM-DD'),
          end_time: dayjs().add(-1, 'week').endOf('week').add(1, 'day').format('YYYY-MM-DD'),
        },
        {
          id: 3,
          name: '本月',
          start_time: dayjs().startOf('month').format('YYYY-MM-DD') ,
          end_time: dayjs().endOf('month').format('YYYY-MM-DD'),
        },
        {
          id: 4,
          name: '上月',
          start_time: dayjs().add(-1, 'month').startOf('month').format('YYYY-MM-DD') ,
          end_time: dayjs().add(-1, 'month').endOf('month').format('YYYY-MM-DD'),
        },
      ]
      let curMonth = dayjs().month() + 1
      if (curMonth < 3) {
        dateTimes.push(
          {
            id: 5,
            name: '本季度',
            start_time: dayjs().month(0).startOf('month').format('YYYY-MM-DD'),
            end_time: dayjs().month(2).endOf('month').format('YYYY-MM-DD'),
          },
          {
            id: 6,
            name: '上季度',
            start_time: dayjs().add(-1, 'year').month(9).format('YYYY-MM-DD'),
            end_time: dayjs().add(-1, 'year').month(11).endOf('month').format('YYYY-MM-DD'),
          },
        )
      } else if (curMonth < 6) {
        dateTimes.push(
          {
            id: 5,
            name: '本季度',
            start_time: dayjs().month(3).format('YYYY-MM-DD'),
            end_time: dayjs().month(5).endOf('month').format('YYYY-MM-DD'),
          },
          {
            id: 6,
            name: '上季度',
            start_time: dayjs().month(0).format('YYYY-MM-DD'),
            end_time: dayjs().month(2).endOf('month').format('YYYY-MM-DD') ,
          },
        )
      } else if (curMonth < 9) {
        dateTimes.push(
          {
            id: 5,
            name: '本季度',
            start_time: dayjs().month(6).format('YYYY-MM-DD'),
            end_time: dayjs().month(8).endOf('month').format('YYYY-MM-DD'),
          },
          {
            id: 6,
            name: '上季度',
            start_time: dayjs().month(3).format('YYYY-MM-DD'),
            end_time: dayjs().month(5).endOf('month').format('YYYY-MM-DD'),
          },
        )
      } else if (curMonth < 12) {
        dateTimes.push(
          {
            id: 5,
            name: '本季度',
            start_time: dayjs().month(9).format('YYYY-MM-DD'),
            end_time: dayjs().month(11).endOf('month').format('YYYY-MM-DD'),
          },
          {
            id: 6,
            name: '上季度',
            start_time: dayjs().month(6).format('YYYY-MM-DD'),
            end_time: dayjs().month(8).endOf('month').format('YYYY-MM-DD'),
          },
        )
      }
以下是Python的datetime实现本周上周本月上月起止日期的代码: ```python import datetime # 本周起止日期 now = datetime.datetime.now() start_of_week = now - datetime.timedelta(days=now.weekday()) end_of_week = start_of_week + datetime.timedelta(days=6) # 上周起止日期 start_of_last_week = start_of_week - datetime.timedelta(days=7) end_of_last_week = end_of_week - datetime.timedelta(days=7) # 本月起止日期 start_of_month = datetime.datetime(now.year, now.month, 1) if now.month == 12: end_of_month = datetime.datetime(now.year+1, 1, 1) - datetime.timedelta(days=1) else: end_of_month = datetime.datetime(now.year, now.month+1, 1) - datetime.timedelta(days=1) # 上月起止日期 last_month = now.month - 1 if now.month > 1 else 12 last_year = now.year - 1 if last_month == 12 else now.year start_of_last_month = datetime.datetime(last_year, last_month, 1) end_of_last_month = datetime.datetime(now.year, now.month, 1) - datetime.timedelta(days=1) # 输出起止日期 print("本周起止日期:", start_of_week.date(), "-", end_of_week.date()) print("上周起止日期:", start_of_last_week.date(), "-", end_of_last_week.date()) print("本月起止日期:", start_of_month.date(), "-", end_of_month.date()) print("上月起止日期:", start_of_last_month.date(), "-", end_of_last_month.date()) ``` 其中关键的方法是`datetime.timedelta()`表示时间差,例如`datetime.timedelta(days=7)`表示7天时间差。`datetime.datetime()`则表示一个具体的日期时间。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值