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.mzph.cn/news/198568.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【每日易题】Leetcode上Hard难度的动态规划题目——地下城游戏的实现

君兮_的个人主页 即使走的再远,也勿忘启程时的初心 C/C 游戏开发 Hello,米娜桑们,这里是君兮_,博主最近一直在钻研动态规划算法,最近在Leetcode上刷题的时候遇到一个Hard难度的动态规划题,今天就借此机会来给大家分享…

安装部署Docker-Compose

随笔记录 目录 1. 要在CentOS 7上安装Docker,操作如下: 1.1 更新系统 1.2 安装Docker依赖 1.3 添加Docker官方存储库 1.4 安装Docker引擎 1.5 启动Docker服务 1.6 验证Docker是否成功安装 1.7 设置Docker开机自启动 1.8 权限问题 2. 安装Docke…

品牌保护与知识产权:跨境电商中的法律挑战与解决方案

随着跨境电商的蓬勃发展,品牌保护和知识产权问题日益成为业界关注的焦点。在全球范围内进行电商业务,涉及到多国法律法规的复杂性,品牌所有者需要面对一系列法律挑战,保护其品牌和知识产权不受侵害。本文将深入探讨跨境电商中面临…

2023年广东工业大学腾讯杯新生程序设计竞赛不知道叫什么名字(前缀和)

需要的是男生女生数量相同,做个转化,女生变成-1,然后求一遍前缀和,我们希望找到最长的满足 s u m ( l , r ) 0 sum(l, r)0 sum(l,r)0的区间也就是 s u m ( r ) − s ( l − 1 ) 0 sum(r) - s(l - 1) 0 sum(r)−s(l−1)0 考虑枚…

L1-016:查验身份证

题目描述 一个合法的身份证号码由17位地区、日期编号和顺序编号加1位校验码组成。校验码的计算规则如下: 首先对前17位数字加权求和,权重分配为:{7,9,10,5,8,4,2&#xf…

quickapp_快应用_系统接口应用

系统接口 在项目中使用到的接口都需要在配置文件manifest.json中声明,不然会报如下警告 [WARN] 请在 manifest.json 文件里声明项目代码中用到的接口: system.storage, service.account, system.package, system.webview[1]检查某app是否在手机上安装 官方文档&a…

前端nginx代理,访问后端接口502问题积锦

项目场景: 项目上线:前端服务器配置了nginx反向代理后,用户发出请求,结果502,本地请求后端服务的时候就正常 解决方案 http状态码502 目前: 前端服务是正常的后端服务也正常那么,就是连结前…

【Flink基础】-- 延迟数据的处理

目录 ​一、关于延迟的一些概念 1、什么是延迟? 2、什么导致互联网延迟?

jenkins清理僵尸任务和排队任务

步骤 进入 Manage Jenkins系统管理 然后点击 Script Console , 然后根据自己需求选择性执行后面的脚本 查看所有进程 Thread.getAllStackTraces().keySet().each() {t -> println("name:"t.getName()) }例如: name:Thread-90 name:Scheduler-174573182-1 name…

Java List、Set、Map 之间的区别是什么?

Java List、Set、Map 之间的区别是什么? 在Java中,List、Set 和 Map 是三种不同的集合接口,用于存储和操作不同类型的数据。 1. List 接口: 特点: 有序:元素的顺序与插入顺序一致。允许重复:…

P1 Orange Pi Zero2镜像烧录和环境搭建(换源)

目录 前言 01 镜像下载 02 镜像烧录 03 SD卡启动 04 SSH登录开发板 05 基于官方外设开发 方法1 方法2 前言 此专栏为智能家居项目,使用的开发板一共有三块,分别为 主控:Orange Pi Zero2触摸屏控制面板:i.MX 6ULL子系统&…

【华为OD题库-063】字符匹配-java

题目 给你一个字符串数组(每个字符串均由小写字母组成)和一个字符规律(由小写字母和.和组成),识别数组中哪些字符串可以匹配到字符规律上。".“匹配任意单个字符,”*"匹配零个或多个前面的那一个元素,所谓匹配&#xff…

字符指针变量数组指针变量

字符指针变量 在指针的类型中&#xff0c;我们知道有一种指针叫做字符指针 一般的使用情况如下&#xff1a; #include<stdio.h> int main() {char ch w;char* pa &ch;*pa h;printf("%c", *pa);return 0; } 还有一种使用方法如下&#xff1a; #incl…

国内 AI 成图第一案!你来你会怎么判?

我国目前并未出台专门针对网络爬虫技术的法律规范&#xff0c;但在司法实践中&#xff0c;相关判决已屡见不鲜&#xff0c;K 哥特设了“K哥爬虫普法”专栏&#xff0c;本栏目通过对真实案例的分析&#xff0c;旨在提高广大爬虫工程师的法律意识&#xff0c;知晓如何合法合规利用…

室内外融合便携式定位终端5G+UWB+RTK

一、介绍 便携式定位终端主要用于提供高精度的位置数据&#xff0c;支持室内UWB定位和室外北斗系统定位功能&#xff0c;支持5G公网和5G专网通信功能&#xff0c;便携式定位终端中超宽带(UWB)和实时动态(RTK)技术的集成代表了精确位置跟踪方面的重大进步。这款UWBRTK便携式定位…

git 本地改动无法删除

1. 问题 记录下git遇到奇怪的问题&#xff0c;本地有些改动不知道什么原因无法删除 git stash&#xff0c; git reset --hard HEAD 等都无法生效&#xff0c;最终通过强制拉取线上解决 如下图&#xff1a; 2. 解决 git fetch --all git reset --hard origin/master执行这两…

Android RatingBar实现五星好评

属性 isIndicatorRatingBar 是否为指示器&#xff0c;为true时&#xff0c;用户将无法交互操作&#xff0c;默认为false。 numStars 显示的星型数量&#xff0c;必须是一个整形值&#xff0c;像“50”&#xff0c;虽然可以设置很大&#xff0c;但一般…

Java拆分集合

在对数据进行批量操作的时候&#xff0c;由于数据量太大&#xff0c;需要对数据进行拆分操作&#xff0c;分成N个小集合。 代码如下&#xff1a; import cn.hutool.core.collection.CollUtil; import cn.hutool.core.util.StrUtil;import java.util.ArrayList; import java.u…

java多线程学习- 下

六. 线程管理 6.1 线程组 类似于在计算机中使用文件夹管理文件, 也可以使用线程组来管理线程,在线程组中定义一组相似(相关)的线程. 在线程组中也可以定义子线程组, Thread 类有几个构造方法允许在创建线程时指定线程组,如果创建线程时没有指定线程组则该线程就属于浮现出所在…

STM32-新建工程(标准库)

目录 STM32F10x新建工程&#xff08;标准库&#xff09; 移植文件夹 新建工程 添加启动文件和必需文件 在工程中加载新添加的文件 在工程中添加文件路径 在工程中添加main函数 添加lib库 添加必需文件 添加宏定义 STM32F10x新建工程&#xff08;标准库&#xff09; …