js获取当前时间,当日零点,前一周时间

项目场景:

根据时间进行数据的快捷筛选


解决方案:

1.获取当前时间和当日零点时间

 //当日
 $("#today").click(function () {
     var currentTime = new Date(Date.now());
     var formattedCurrentTime = currentTime.toLocaleString('zh-CN', { year: 'numeric', month: '2-digit', day: '2-digit', hour: '2-digit', minute: '2-digit', second: '2-digit' }).replace(/年|月/g, "-").replace(/日/g, " ");
   var  EndDate = formattedCurrentTime.replace(/\//g, "-"); // 将斜线替换为横线

     // 获取当天零点时间并以指定格式展示
     var currentZero = new Date();
     currentZero.setHours(0, 0, 0, 0);
     var formattedZeroTime = currentZero.toLocaleString('zh-CN', { year: 'numeric', month: '2-digit', day: '2-digit', hour: '2-digit', minute: '2-digit', second: '2-digit' }).replace(/年|月/g, "-").replace(/日/g, " ");
    var  StarDate = formattedZeroTime.replace(/\//g, "-"); // 将斜线替换为横线

     console.log("当前时间:", EndDate);
     console.log("当天零点时间:", StarDate);

     LoadHeatStationCharTime(StarDate, EndDate)
 });

2.获取上周时间

 $("#btnUp").click(function () {
     // 获取当天所在月、日、周
     var week = "";
     var lastMondayMonth = "";
      var lastMondayDate = "";
     var lastMondayYear = "";
   var  getDataTime = "";
   var  currentDate = "";
     // 获取当前星期(0-6,0代表星期天)
     if (new Date().getDay() === 0) {
         week = '星期日'
          lastMonday = new Date(new Date().setDate(new Date().getDate() - 6));
          lastMondayMonth = lastMonday.getMonth() + 1;
         lastMondayDate = lastMonday.getDate();
         lastMondayYear = lastMonday.getFullYear();
         StarDate = lastMondayYear + "-" + lastMondayMonth + "-" + lastMondayDate+ " 00:00:00";
     }
     
     if (new Date().getDay() === 1) {
         week = '星期一'
          lastMonday = new Date(new Date().setDate(new Date().getDate() - 7));
          lastMondayMonth = lastMonday.getMonth() + 1;
          lastMondayDate = lastMonday.getDate();
         lastMondayYear = lastMonday.getFullYear();
         StarDate = lastMondayYear + "-" + lastMondayMonth + "-" + lastMondayDate + " 00:00:00";
         console.log("前几天的日期:" + StarDate);
     }
     
     if (new Date().getDay() === 2) {
         week = '星期二'
          lastMonday = new Date(new Date().setDate(new Date().getDate() - 1));
          lastMondayMonth = lastMonday.getMonth() + 1;
         lastMondayDate = lastMonday.getDate();
         lastMondayYear = lastMonday.getFullYear();
         StarDate = lastMondayYear + "-" + lastMondayMonth + "-" + lastMondayDate + " 00:00:00";
         
     }
    
     if (new Date().getDay() === 3) {
         week = '星期三'
          lastMonday = new Date(new Date().setDate(new Date().getDate() - 2));
          lastMondayMonth = lastMonday.getMonth() + 1;
         lastMondayDate = lastMonday.getDate();
         lastMondayYear = lastMonday.getFullYear();
         StarDate = lastMondayYear + "-" + lastMondayMonth + "-" + lastMondayDate + " 00:00:00";
     }
     
     if (new Date().getDay() === 4) {
         week = '星期四'
          lastMonday = new Date(new Date().setDate(new Date().getDate() - 3));
          lastMondayMonth = lastMonday.getMonth() + 1;
         lastMondayDate = lastMonday.getDate();
         lastMondayYear = lastMonday.getFullYear();
         StarDate = lastMondayYear + "-" + lastMondayMonth + "-" + lastMondayDate + " 00:00:00";
     }
    
     if (new Date().getDay() === 5) {
         week = '星期五'
          lastMonday = new Date(new Date().setDate(new Date().getDate() - 4));
          lastMondayMonth = lastMonday.getMonth() + 1;
         lastMondayDate = lastMonday.getDate();
         lastMondayYear = lastMonday.getFullYear();
         StarDate = lastMondayYear + "-" + lastMondayMonth + "-" + lastMondayDate + " 00:00:00";
     }
     
     if (new Date().getDay() === 6) {
         week = '星期六'
          lastMonday = new Date(new Date().setDate(new Date().getDate() - 5));
          lastMondayMonth = lastMonday.getMonth() + 1;
         lastMondayDate = lastMonday.getDate();
         lastMondayYear = lastMonday.getFullYear();
         StarDate = lastMondayYear + "-" + lastMondayMonth + "-" + lastMondayDate + " 00:00:00";
     }
     var formattedCurrentTime = currentTime.toLocaleString('zh-CN', { year: 'numeric', month: '2-digit', day: '2-digit', hour: '2-digit', minute: '2-digit', second: '2-digit' }).replace(/年|月/g, "-").replace(/日/g, " ");
     EndDate = formattedCurrentTime.replace(/\//g, "-"); // 将斜线替换为横线
    
    // console.log(EndDate)
     
     LoadHeatStationCharTime(StarDate, EndDate)
 });

相关推荐

  1. js获取当前时间,当日零点,时间

    2023-12-05 18:14:05       35 阅读
  2. js获取当前时间

    2023-12-05 18:14:05       38 阅读
  3. Qt - 获取系统当前时间

    2023-12-05 18:14:05       12 阅读
  4. JNI中获取当前时间

    2023-12-05 18:14:05       31 阅读
  5. 通过Django实现获取当前时间

    2023-12-05 18:14:05       28 阅读
  6. PHP 日期和时间 Date()函数 获取当前时间

    2023-12-05 18:14:05       11 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-05 18:14:05       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-05 18:14:05       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-05 18:14:05       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-05 18:14:05       20 阅读

热门阅读

  1. PyTorch:模型加载方法详解

    2023-12-05 18:14:05       36 阅读
  2. QT基础教程(文本绘制)

    2023-12-05 18:14:05       41 阅读
  3. Verilog基本语法概述

    2023-12-05 18:14:05       39 阅读
  4. 3台4核16G机器搭建K8S集群

    2023-12-05 18:14:05       42 阅读
  5. element-UI中el-scrollbar的使用

    2023-12-05 18:14:05       38 阅读
  6. OWASP Web 安全测试指南-Web 应用程序安全测试

    2023-12-05 18:14:05       39 阅读
  7. Vue3使用阿里云OSS直传

    2023-12-05 18:14:05       42 阅读
  8. 【无标题】

    2023-12-05 18:14:05       47 阅读