当前位置: 首页 > article >正文

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)
 });


http://www.kler.cn/a/157569.html

相关文章:

  • web自动化测试知识总结
  • es使用knn向量检索中numCandidates和k应该如何配比更合适
  • 基础数据结构---栈
  • C缺陷与陷阱 — 8 编译与链接
  • WSL Ubuntu
  • DataOps驱动数据集成创新:Apache DolphinScheduler SeaTunnel on Amazon Web Services
  • Web测试自动化工具Selenium的使用
  • Java中熟练掌握BigDecimal运用-工具类
  • netcore swagger 错误 Failed to load API definition
  • 【开源】基于Vue+SpringBoot的康复中心管理系统
  • 【Unity动画】Unity 动画播放的流程
  • Python处理Point, MultiPolygon, Polygon, LineString等Geo地理形状数据
  • 根据已有安装的cuda配置合适的pytorch环境
  • vue:ESLint如何自动修复代码
  • IntelliJ IDEA的下载安装配置步骤详解
  • 开源堡垒机Jumpserver
  • 人工智能|网络爬虫——用Python爬取电影数据并可视化分析
  • 记录33333
  • 大数据项目——基于Django协同过滤算法的房源可视化分析推荐系统的设计与实现
  • 图像万物分割——Segment Anything算法解析与模型推理
  • 【NGINX--12】性能调优
  • WPF halcon 机器视觉
  • 【安卓】安卓xTS之Media模块 学习笔记(1) xTS介绍
  • C语言小游戏:三子棋
  • MATLAB算法实战应用案例精讲-【数模应用】机器学习-聚类算法
  • 【物联网】Node-RED