项目场景:
解决方案:
//当日
$(“#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);
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)
});
原文地址:https://blog.csdn.net/m0_60741007/article/details/134787806
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.7code.cn/show_47224.html
如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!