当前位置:网站首页>js时间获取本周一、周日,判断时间是今天,今天前、后
js时间获取本周一、周日,判断时间是今天,今天前、后
2022-04-23 06:05:00 【念雅】
记录最近在大屏展示需求中,用到的一些时间处理方法。
一、获取今天的前3天,后3天
逻辑:
1、根据今天的24点时间戳,加上对应的时间戳范围即为今天的后多少天。
2、今天0点时间戳,减去对应时间戳,即为今天的前多少天。
// 获取当天 0 点的时间戳
let timeStamp = new Date(new Date().setHours(0, 0, 0, 0)) / 1000;
// 一天是86400秒 故 3 天前的时间戳为
let threeDayAgo1 = timeStamp - 86400 * 3;
// 一天是86400秒 故 3 天后的时间戳为
let fourDayLater1 = timeStamp + 86400 * 4;
let threeDayAgo = moment(new Date()).add(-3, 'days').format('YYYYMMDD');
let threeDayLater = moment(new Date()).add(+3, 'days').format('YYYYMMDD');
二、获取本周一、周日
var now = new Date(); //当前日期
var nowDayOfWeek = now.getDay(); //今天本周的第几天
var nowYear = now.getFullYear(); //当前年
var nowMonth = now.getMonth(); //当前月
var nowDay = now.getDate(); //当前日
//格式化日期:yyyyMMdd,这里可以根据需要做相关格式处理
function formatDate(date) {
var currentYear = date.getFullYear();
var currentMonth = date.getMonth()+1;
var currentWeekday = date.getDate();
if(currentMonth < 10){//小于10,补0
currentMonth = "0" + currentMonth;
}
if(currentWeekday < 10){//小于10,补0
currentWeekday = "0" + currentWeekday;
}
return (currentYear+''+currentMonth+''+currentWeekday);
}
//获得本周的开始日期,加1
function getWeekStartDate() {
var weekStartDate = new Date(nowYear, nowMonth, nowDay-nowDayOfWeek+1);//因为本周开始日期日历认为是周日,所以开始日期加了一天,
return formatDate(weekStartDate);
}
//获得本周的结束日期,加一
function getWeekEndDate() {
var weekEndDate = new Date(nowYear, nowMonth, nowDay + (6 - nowDayOfWeek)+1);//因为本周结束日期日历认为是周六,所以结束日期加了一天,
return formatDate(weekEndDate);
}
二、判断时间是今天、之前、之后
逻辑:
1、判断时间戳是在今天的0点到24点之间,今天;
2、在今天0点之前,则是今天之前;
3、今天24点之后,则是今天之后。
//处理时间 是不是今天
const timeTodayJudge = (times) => {
//因为传入的值是string "20211130",做相应的字符分割
const year = String(times).substring(0, 4);
const month = String(times).substring(4, 6);
const day = String(times).substring(6, 8);
let time = `${year}-${month}-${day}`;
let timestamp = Date.parse(time);//数据的时间戳
let todayOStamp = new Date(new Date().toLocaleDateString()).getTime();//当天0点时间戳
let today24Stamp = new Date(new Date(new Date().toLocaleDateString()).getTime() + 24 * 60 * 60 * 1000 - 1);//当天24点时间戳
// console.log("===timestamp", timestamp);
// console.log("===new Date().toDateString()", new Date().toDateString())
if (timestamp > todayOStamp && timestamp < today24Stamp) {
return 'today';
} else if (timestamp < todayOStamp) {//今天之前
return 'left';
} else if (timestamp > today24Stamp) {//今天之后
return 'right';
}
}
版权声明
本文为[念雅]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_43865681/article/details/121693825
边栏推荐
猜你喜欢

Winter combat camp hands-on combat - cloud essential environment preparation, hands-on practical operation, quickly build lamp environment, lead mouse cloud Xiaobao backpack without shadow

基于ECS搭建云上博客(云小宝码上送祝福,免费抽iphone13任务详解)

Redis practice notes and source code analysis

Prometheus cortex Architecture Overview (horizontally scalable, highly available, multi tenant, long-term storage)

【机器学习】笔记 4、KNN+交叉验证

Dolphinscheduler调度spark任务踩坑记录

Chaos vous emmène au projet chaos.

Try catch cannot catch asynchronous errors

Information:2021/9/29 10:01 - Build completed with 1 error and 0 warnings in 11s 30ms Error异常处理

Thanos compact component test summary (processing historical data)
随机推荐
Apache SeaTunnel 2.1.0部署及踩坑
数据库的事务
Prometheus alarm record persistence (historical alarm saving and Statistics)
Dolphinscheduler调度sql任务建表时The query did not generate a result set异常解决
Chaos vous emmène au projet chaos.
Thanos compact component test summary (processing historical data)
oracle库恢复数据
异常记录-19
Build a cloud blog based on ECS (polite experience)
PG SQL截取字符串到指定字符位置
qs.stringify 接口里把入参转为&连接的字符串(配合application/x-www-form-urlencoded请求头)
mysql和pg库遇到冲突数据时的两种处理方式
oracle通过触发器和序列来定义自增主键,并且设置定时任务每秒钟插入一条数据到目标表
Introduction to common APIs for EBFP programming
BPF program of type XDP
oracle undo使用率高问题处理
Try catch cannot catch asynchronous errors
oracle计算两日期相差多少秒,分钟,小时,天
Alertmanager重复/缺失告警现象探究及两个关键参数group_wait和group_interval的释义
Prometheus和Thanos Receiver的“写多租户”实现