获取本周第一天和最后一天&本月第一天和最后一天&本年第一天最后一天&昨天&前天日期

本文介绍了一个JavaScript代码片段,用于正确获取当前周、月和年的日期范围,并针对周日时获取下周日期的问题进行了修复。同时,提供了一个获取昨天和前天日期的方法。代码已进行了逻辑优化,确保按照周一至周日为一周的标准进行计算。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

评论有小伙伴提出周日的时候获取的不对,我自己后来也发现了,周日的时候会获取到下周 周一-周日的日期,主要是date默认的一周是周日-周六,所以周日的时候date.getDay()获取到的是本周的第一天,而我的逻辑是按照周一--周日,所以本周日的时候会获取了下周一--下周日的日期

有需要的小伙伴可以按照下面调整一下逻辑为周日--周六为一周就没问题了

getCurrentDay(currentDate) {
      let thisweek = {}
      let thisMonth = {}
      let thisYear = {}
      let date = new Date();
      // 本周一的日期
      date.setDate(date.getDate() - date.getDay() + 1);
      thisweek.start_day = date.getFullYear() + "-" + ((date.getMonth() + 1)<10?"0"+(date.getMonth() + 1):(date.getMonth() + 1)) + "-" + (date.getDate()<10?"0"+date.getDate():date.getDate()) ;
      // 本周日的日期
      date.setDate(date.getDate() - date.getDay() + 7);
      thisweek.end_day = date.getFullYear() + "-" + ((date.getMonth() + 1)<10?"0"+(date.getMonth() + 1):(date.getMonth() + 1))  + "-" + (date.getDate()<10?"0"+date.getDate():date.getDate());

      // 本月第一天
      date.setDate(1);
      thisMonth.start_day = date.getFullYear() + "-" + ((date.getMonth() + 1)<10?"0"+(date.getMonth() + 1):(date.getMonth() + 1))  + "-" + (date.getDate()<10?"0"+date.getDate():date.getDate())
      // 本月最后一天
      var lastDay = new Date(date.getFullYear(), date.getMonth() + 1 , 0)
      thisMonth.end_day = lastDay.getFullYear() + "-" + ((lastDay.getMonth() + 1)<10?"0"+(lastDay.getMonth()+1):(lastDay.getMonth() + 1))  + "-" + (lastDay.getDate()<10?"0"+lastDay.getDate():lastDay.getDate())

      // 本年第一天
      thisYear.start_day = date.getFullYear() + "-" + "01-01"
      // 本年最后一天
      thisYear.end_day = date.getFullYear() + "-" + "12-31"
      
      if(currentDate=="本周"){
        return thisweek
      }else if(currentDate=="本月"){
        return thisMonth
      }else if(currentDate=="本年"){}
        return thisYear
    }

百度了n个大神的方法终于整合了这一个方法

再补充一个获取昨天前天的方法

getDate(){
    var nowDate = new Date();
    var year = nowDate.getFullYear();
    var month = nowDate.getMonth() + 1 < 10 ? "0" + (nowDate.getMonth() + 1) : nowDate.getMonth() + 1;
    var date = nowDate.getDate() < 10 ? "0" + nowDate.getDate() : nowDate.getDate();
    let yesterday = year +"-"+ month +"-" + (date-1)
    let beforeYesterday = year +"-"+ month +"-" + (date-2)
    if(date=="01"){
      let day = new Date(nowDate.getFullYear(), nowDate.getMonth(), 0).getDate();
      yesterday = year +"-"+ ((month-1)<10?"0" + (month-1):(month-1)) +"-" + day
      beforeYesterday = year +"-"+ ((month-1)<10?"0" + (month-1):(month-1)) +"-" + (day-1)
    }else if(date=="02"){
      let day = new Date(nowDate.getFullYear(), nowDate.getMonth(), 0).getDate();
      beforeYesterday = year +"-"+ ((month-1)<10?"0" + (month-1):(month-1)) +"-" + day
    }
    this.setState({
      yesterday: yesterday,
      beforeYesterday: beforeYesterday
    })
  }

csdn文章推荐受影响解决办法10个字10行

csdn文章推荐受影响解决办法10个字10行

csdn文章推荐受影响解决办法10个字10行

csdn文章推荐受影响解决办法10个字10行

csdn文章推荐受影响解决办法10个字10行

csdn文章推荐受影响解决办法10个字10行

csdn文章推荐受影响解决办法10个字10行

csdn文章推荐受影响解决办法10个字10行

csdn文章推荐受影响解决办法10个字10行

csdn文章推荐受影响解决办法10个字10行

你可以使用 MySQL 的内置函数 `DATE_FORMAT()` `NOW()` 来获取本周第一天本月第一天本年第一天的值。 查询本周第一天的 SQL 语句如下: ``` SELECT DATE_FORMAT(NOW() - INTERVAL WEEKDAY(NOW()) DAY ,&#39;%Y-%m-%d&#39;); ``` 查询本月第一天的 SQL 语句如下: ``` SELECT DATE_FORMAT(NOW() ,&#39;%Y-%m-01&#39;); ``` 查询本年第一天的 SQL 语句如下: ``` SELECT DATE_FORMAT(NOW() ,&#39;%Y-01-01&#39;); ``` 在 MyBatis 的映射文件 `TrendMapper.xml` 中,你需要添加三个参数,分别对应本周第一天本月第一天本年第一天的值。然后在 SQL 语句中使用这三个参数来替换原来的日期值。 修改后的 SQL 语句如下: ``` SELECT DATE_FORMAT(statistics_date, &#39;%Y-%m-%d&#39;) as statisticsDate, COUNT(*) as lotteryWinCount FROM t_lottery_statistics_prize WHERE statistics_date BETWEEN #{weekBegin} AND NOW() AND statistics_date &gt;= #{monthBegin} AND statistics_date &gt;= #{yearBegin} GROUP BY DATE_FORMAT(statistics_date, &#39;%Y-%m-%d&#39;) ORDER BY DATE_FORMAT(statistics_date, &#39;%Y-%m-%d&#39;) DESC ``` 其中,`#{weekBegin}`、`#{monthBegin}` `#{yearBegin}` 分别对应本周第一天本月第一天本年第一天的参数。 在 Java 代码中,你需要定义这三个参数的值,并将它们传递给 MyBatis 的 SQL 语句。 示例代码如下: ```java public List&lt;Trend&gt; selectTrend() { Date weekBegin = ... // 计算本周第一天的值 Date monthBegin = ... // 计算本月第一天的值 Date yearBegin = ... // 计算本年第一天的值 Map&lt;String, Object&gt; paramMap = new HashMap&lt;&gt;(); paramMap.put(&quot;weekBegin&quot;, weekBegin); paramMap.put(&quot;monthBegin&quot;, monthBegin); paramMap.put(&quot;yearBegin&quot;, yearBegin); return sqlSession.selectList(&quot;com.yuxiang.dtsociety.lottery.dao.TrendDao.selectTrend&quot;, paramMap); } ``` 其中,`weekBegin`、`monthBegin` `yearBegin` 参数是新增的。在 `paramMap` 中,将这三个参数都传递给 MyBatis 的 SQL 语句。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值