获取当天,本周,本月,本季度,本半年,本年时间

1. 使用场景

elemet-plus 组件库,日期选择器快捷使用选择本月,本季度时间…

2. 方法

const useDate = () => {
  const now = new Date() // 当前日期
  const nowYear = now.getFullYear() //当前年
  const nowMonth = now.getMonth() //当前月
  const nowDay = now.getDate() // 当前日
  const nowDayOfWeek = now.getDay() // 周几
  const jd = Math.ceil((nowMonth + 1) / 3) // 当前季度

  // 当天
  function getCurDay() {
    return [now, now]
  }

  // 本周
  function getCurWeek() {
    const startDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek)
    const endDate = new Date(nowYear, nowMonth, nowDay + (6 - nowDayOfWeek))
    return [startDate, endDate]
  }
  // 本月
  function getCurMonth() {
    const startDate = new Date(nowYear, nowMonth, 1)
    const endDate = new Date(nowYear, nowMonth + 1, 0)
    return [startDate, endDate]
  }
  
  // 本季度
  function getCurQuarter() {
    const startDate = new Date(nowYear, (jd - 1) * 3, 1)
    const endDate = new Date(nowYear, jd * 3, 0)
    return [startDate, endDate]
  }
  
  // 本半年
  function getCurHalfYear() {
    const startD = new Date(nowYear, nowMonth - 4, 1)
    const startDate = new Date(nowYear, 0, 1)
    const endD = new Date(nowYear, nowMonth + 1, 0)
    const year = startD.getFullYear()
    if (year === nowYear) {
      return [startD, endD]
    } else {
      return [startDate, endD]
    }
  }
  
  // 本年
  function getCurYear() {
    const startDate = new Date(nowYear, 0, 1)
    const endDate = new Date(nowYear, 11, 0)
    return [startDate, endDate]
  }

  return {
    getCurDay,
    getCurWeek,
    getCurMonth,
    getCurQuarter,
    getCurHalfYear,
    getCurYear,
  }
}

const {
  getCurDay,
  getCurWeek,
  getCurMonth,
  getCurQuarter,
  getCurHalfYear,
  getCurYear,
} = useDate()

export const shortcuts = [
  {
    text: '当天',
    value: getCurDay(),
  },
  {
    text: '本周',
    value: getCurWeek(),
  },
  {
    text: '本月',
    value: getCurMonth(),
  },
  {
    text: '本季度',
    value: getCurQuarter(),
  },
  {
    text: '半年',
    value: getCurHalfYear(),
  },
  {
    text: '当年',
    value: getCurYear(),
  },
]

如有疑问欢迎留言交流,有更好的方法希望不吝赐教~

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
好的,我理解了你的问题。使用 LambdaQueryWrapper 根据当前时间查询数据库中当日、本周本月本年的数据,可以通过以下方式实现: 1. 当日数据查询: ```java // 获取当日的起止时间 LocalDateTime now = LocalDateTime.now(); LocalDateTime start = LocalDateTime.of(now.toLocalDate(), LocalTime.MIN); LocalDateTime end = LocalDateTime.of(now.toLocalDate(), LocalTime.MAX); // 构建查询条件 LambdaQueryWrapper<Entity> queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.between("create_time", start, end); // 执行查询 List<Entity> entityList = entityMapper.selectList(queryWrapper); ``` 2. 本周数据查询: ```java // 获取本周的起止时间 LocalDateTime now = LocalDateTime.now(); LocalDateTime start = now.with(DayOfWeek.MONDAY).with(LocalTime.MIN); LocalDateTime end = now.with(DayOfWeek.SUNDAY).with(LocalTime.MAX); // 构建查询条件 LambdaQueryWrapper<Entity> queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.between("create_time", start, end); // 执行查询 List<Entity> entityList = entityMapper.selectList(queryWrapper); ``` 3. 本月数据查询: ```java // 获取本月的起止时间 LocalDateTime now = LocalDateTime.now(); LocalDateTime start = now.with(TemporalAdjusters.firstDayOfMonth()).with(LocalTime.MIN); LocalDateTime end = now.with(TemporalAdjusters.lastDayOfMonth()).with(LocalTime.MAX); // 构建查询条件 LambdaQueryWrapper<Entity> queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.between("create_time", start, end); // 执行查询 List<Entity> entityList = entityMapper.selectList(queryWrapper); ``` 4. 本年数据查询: ```java // 获取本年的起止时间 LocalDateTime now = LocalDateTime.now(); LocalDateTime start = now.with(TemporalAdjusters.firstDayOfYear()).with(LocalTime.MIN); LocalDateTime end = now.with(TemporalAdjusters.lastDayOfYear()).with(LocalTime.MAX); // 构建查询条件 LambdaQueryWrapper<Entity> queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.between("create_time", start, end); // 执行查询 List<Entity> entityList = entityMapper.selectList(queryWrapper); ``` 以上代码中,`Entity` 是你要查询的实体类,`entityMapper` 是对应的 Mapper 接口。我们首先获取当前时间,然后根据当前时间计算出时间范围,最后构建查询条件并执行查询

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值