当前位置:网站首页>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
边栏推荐
- 关于Postgres主从复制延迟监控的错误告警问题
- oracle库恢复数据
- Alertmanager重复/缺失告警现象探究及两个关键参数group_wait和group_interval的释义
- 解决:You have 18 unapplied migration(s). Your project may not work properly until you apply
- Dolphinscheduler配置Datax踩坑记录
- 解决::Argument ‘radius‘ is required to be an integer
- 异常记录-16
- Practice using polardb and ECs to build portal websites
- 基于ECS搭建云上博客(云小宝码上送祝福,免费抽iphone13任务详解)
- Error alarm of Postgres master-slave replication delay monitoring
猜你喜欢
Chaos takes you to the chaos project quickly
Information:2021/9/29 10:01 - Build completed with 1 error and 0 warnings in 11s 30ms Error异常处理
Introduction to RDMA
Virtio and Vhost_ Net introduction
Introduction to common APIs for EBFP programming
Oracle redo log产生量大的查找思路与案例
Apache Atlas 编译及安装记录
BPF program of type XDP
冬季实战营 动手实战-初识上云基础,动手实操ECS云服务器新手上路 领鼠标 云小宝 背包 无影
Using Prom label proxy to implement label based multi tenant reading of Prometheus thanos
随机推荐
Introduction to common APIs for EBFP programming
Oracle redo log产生量大的查找思路与案例
Exception record-9
Chaos带你快速上手混沌工程
oracle表空间表分区详解及oracle表分区查询使用方法
oracle库恢复数据
Prometheus thanos Quick Guide
oracle通过触发器和序列来定义自增主键,并且设置定时任务每秒钟插入一条数据到目标表
How does thanos configure different data retention durations for different tenants
Abnormal record-15
Winter combat camp hands-on combat - MySQL database rapid deployment practice lead mouse cloud Xiaobao
mysql和pgsql时间相关操作
How does VirtualBox modify the IP network segment assigned to the virtual machine in the "network address translation (NAT)" network mode
Implementation of multi tenant read and write in Prometheus cortex
Abnormal record-11
Oracle锁表查询和解锁方法
异常记录-19
VirtualBox如何修改“网络地址转换(NAT)”网络模式下分配给虚拟机的IP网段
Abnormal record-20
Introduction to RDMA