js获取本周,本月,本年,上周,上月,去年的时间

function getTime(type){
  let now = new Date();
  let nowDayOfWeek = now.getDay(); //今天本周的第几天
  let nowDay = now.getDate(); //当前日
  let nowMonth = now.getMonth() + 1; //当前月  下标从0开始
  let nowYear = now.getFullYear(); //当前年
  let d = new Date(nowYear, (nowMonth + 1), 0).getDate();

  //上月
  let lastMonthDate = new Date(); //上月日期
  lastMonthDate.setDate(1);
  lastMonthDate.setMonth(lastMonthDate.getMonth()-1);
  let lastMonthYear = lastMonthDate.getFullYear();
  let lastMonth = lastMonthDate.getMonth() + 1;

  let lastYear = nowYear - 1;


  let weekStart = new Date(nowYear, nowMonth - 1, nowDay  - nowDayOfWeek + 1);
  let weekEnd = new Date(nowYear, nowMonth - 1, nowDay + (7 - nowDayOfWeek));

  let time: [string, string, string] = ["", "", "0"];
  switch (type) {
    case "0":
      break;
    //本周
    case "1":
      time = [formatDate(weekStart), formatDate(now), "4"];
      break;
    //本月
    case "2":
      let month = nowMonth < 10 ? '0' + nowMonth : nowMonth
      time = [nowYear + "-" + month + "-01", formatDate(now), "4"];
      break;
    //本年
    case "3":
      time = [nowYear + "-01-01", formatDate(now), "2"];
      break;
    //上周
    case "4":
      time = [formatDate(new Date(nowYear, nowMonth-1, nowDay - nowDayOfWeek - 6)), formatDate(new Date(nowYear, nowMonth-1, nowDay - nowDayOfWeek)), "4"];
      break;
    //上月
    case "5":
      time = [formatDate(new Date(lastMonthYear, lastMonth, 1)), formatDate(new Date(lastMonthYear, lastMonth, d)), "4"];
      break;
    //上年
    case "6":
      time = [lastYear + "-01-01", lastYear + "-12-31", "2"];
      break;
  }
  return time;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我理解了你的问题。使用 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 接口。我们首先获取当前时间,然后根据当前时间计算出时间范围,最后构建查询条件并执行查询。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值