当前位置:网站首页>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
边栏推荐
猜你喜欢
Build a cloud blog based on ECS (polite experience)
Analysis of Rdam principle
oracle通过触发器和序列来定义自增主键,并且设置定时任务每秒钟插入一条数据到目标表
Prometheus Thanos快速指南
Thanos Compact组件测试总结(处理历史数据)
Dolphinscheduler配置Datax踩坑记录
Build a cloud blog based on ECS (send blessings on the cloud Xiaobao code and draw iphone13 for free)
Construire un blog Cloud basé sur ECS (bénédiction sur le Code Cloud Xiaobao, explication détaillée de la tâche iphone13 gratuite)
实践使用PolarDB和ECS搭建门户网站
qs.stringify 接口里把入参转为&连接的字符串(配合application/x-www-form-urlencoded请求头)
随机推荐
mysql和pg库遇到冲突数据时的两种处理方式
Analysis of Rdam principle
Memcached source code analysis
Abnormal record-16
RAC环境中openssh版本对SSH互信创建的影响
Exception record-6
Arranges the objects specified in the array in front of the array
Redis FAQ
oracle分区的相关操作
JS function package foreach use return can not jump out of the outer function
Prometheus和Thanos Receiver的“写多租户”实现
TC ebpf practice
"Write multi tenant" implementation of Prometheus and thanos receiver
【机器学习】笔记 4、KNN+交叉验证
Prometheus Cortex架构概述(水平可扩展、高可用、多租户、长期存储)
基于ECS搭建云上博客(云小宝码上送祝福,免费抽iphone13任务详解)
Oracle RAC数据库实例启动异常问题分析IPC Send timeout
异常记录-20
阿里云日志服务sls的典型应用场景
异常记录-15