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)

});

相关推荐
Bdygsl1 小时前
前端开发:JavaScript(3)—— 选择与循环
开发语言·javascript·ecmascript
HW-BASE2 小时前
《C语言》指针练习题--1
c语言·开发语言·单片机·算法·c
max5006002 小时前
基于深度学习的污水新冠RNA测序数据分析系统
开发语言·人工智能·python·深度学习·神经网络
杨荧5 小时前
基于大数据的美食视频播放数据可视化系统 Python+Django+Vue.js
大数据·前端·javascript·vue.js·spring boot·后端·python
ze言5 小时前
为什么现代 C++ (C++11 及以后) 推荐使用 constexpr和模板 (Templates) 作为宏 (#define) 的替代品?
开发语言·c++
cmdyu_5 小时前
如何解决用阿里云效流水线持续集成部署Nuxt静态应用时流程卡住,进行不下去的问题
前端·经验分享·ci/cd
WordPress学习笔记5 小时前
根据浏览器语言判断wordpress访问不同语言的站点
前端·javascript·html
yuanmenglxb20046 小时前
解锁webpack核心技能(二):配置文件和devtool配置指南
前端·webpack·前端工程化
鲸落落丶6 小时前
JavaScript构建工具
javascript