当前位置:网站首页>JS time to get this Monday and Sunday, judge the time is today, before and after today
JS time to get this Monday and Sunday, judge the time is today, before and after today
2022-04-23 13:39:00 【Nianya】
Record the recent demand for large screen display , Some time processing methods used .
One 、 Get today's pre 3 God , after 3 God
Logic :
1、 According to today's 24 Time stamp , Add the corresponding time stamp, and the range is the next few days of today .
2、 today 0 Time stamp , Subtract the corresponding timestamp , That is, how many days before today .
// Get the day 0 Time stamp of the point
let timeStamp = new Date(new Date().setHours(0, 0, 0, 0)) / 1000;
// One day is 86400 second so 3 The timestamp of days ago is
let threeDayAgo1 = timeStamp - 86400 * 3;
// One day is 86400 second so 3 The timestamp after days is
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');
Two 、 Get this Monday 、 Sunday
var now = new Date(); // The current date
var nowDayOfWeek = now.getDay(); // Today is the third day of the week
var nowYear = now.getFullYear(); // The current year
var nowMonth = now.getMonth(); // The current month
var nowDay = now.getDate(); // Current day
// Format date :yyyyMMdd, Here you can do relevant format processing as needed
function formatDate(date) {
var currentYear = date.getFullYear();
var currentMonth = date.getMonth()+1;
var currentWeekday = date.getDate();
if(currentMonth < 10){// Less than 10, repair 0
currentMonth = "0" + currentMonth;
}
if(currentWeekday < 10){// Less than 10, repair 0
currentWeekday = "0" + currentWeekday;
}
return (currentYear+''+currentMonth+''+currentWeekday);
}
// Get the start date of the week , Add 1
function getWeekStartDate() {
var weekStartDate = new Date(nowYear, nowMonth, nowDay-nowDayOfWeek+1);// Because the start date of this week is considered Sunday in the calendar , So add one day to the start date ,
return formatDate(weekStartDate);
}
// Get the end date of the week , Add one
function getWeekEndDate() {
var weekEndDate = new Date(nowYear, nowMonth, nowDay + (6 - nowDayOfWeek)+1);// Because the end date of the week is considered Saturday in the calendar , So add one day to the end date ,
return formatDate(weekEndDate);
}
Two 、 The judgment time is today 、 Before 、 after
Logic :
1、 Judge whether the timestamp is in today's 0 Point to 24 Between points , today ;
2、 In today's 0 Before the point , Before today ;
3、 today 24 After the point , After today .
// The processing time Is it today
const timeTodayJudge = (times) => {
// Because the value passed in is string "20211130", Do the corresponding character segmentation
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);// The timestamp of the data
let todayOStamp = new Date(new Date().toLocaleDateString()).getTime();// same day 0 Time stamp
let today24Stamp = new Date(new Date(new Date().toLocaleDateString()).getTime() + 24 * 60 * 60 * 1000 - 1);// same day 24 Time stamp
// console.log("===timestamp", timestamp);
// console.log("===new Date().toDateString()", new Date().toDateString())
if (timestamp > todayOStamp && timestamp < today24Stamp) {
return 'today';
} else if (timestamp < todayOStamp) {// Before today,
return 'left';
} else if (timestamp > today24Stamp) {// After today
return 'right';
}
}
版权声明
本文为[Nianya]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230605115707.html
边栏推荐
- Use of GDB
- Short name of common UI control
- 2021年6月程序员工资统计,平均15052元,你拖后腿了吗?
- Stack protector under armcc / GCC
- Feature Engineering of interview summary
- Detailed explanation of ADB shell top command
- 解决Oracle中文乱码的问题
- Summary of request and response and their ServletContext
- 在 pytorch 中加载和使用图像分类数据集 Fashion-MNIST
- Solve the problem that Oracle needs to set IP every time in the virtual machine
猜你喜欢

MySQL 8.0.11下载、安装和使用可视化工具连接教程

MySQL 8.0.11 download, install and connect tutorials using visualization tools

9419 page analysis of the latest first-line Internet Android interview questions

Riscv MMU overview

Solve the problem of Oracle Chinese garbled code

你和42W奖金池,就差一次“长沙银行杯”腾讯云启创新大赛!

Loading and using image classification dataset fashion MNIST in pytorch

QT calling external program

Esp32 vhci architecture sets scan mode for traditional Bluetooth, so that the device can be searched

kettle庖丁解牛第16篇之输入组件周边讲解
随机推荐
MySQL 8.0.11下载、安装和使用可视化工具连接教程
Migrating your native/mobile application to Unified Plan/WebRTC 1.0 API
Using open to open a file in JNI returns a - 1 problem
QT调用外部程序
[official announcement] Changsha software talent training base was established!
[point cloud series] foldingnet: point cloud auto encoder via deep grid deformation
Stack protector under armcc / GCC
RTOS mainstream assessment
The interviewer dug a hole for me: how many concurrent TCP connections can a single server have?
[point cloud series] so net: self organizing network for point cloud analysis
SAP ui5 application development tutorial 72 - trial version of animation effect setting of SAP ui5 page routing
Machine learning -- naive Bayes
Mysql数据库的卸载
Riscv MMU overview
LeetCode_ DFS_ Medium_ 695. Maximum area of the island
playwright控制本地谷歌浏览打开,并下载文件
web三大组件之Servlet
TIA博途中基于高速计数器触发中断OB40实现定点加工动作的具体方法示例
联想拯救者Y9000X 2020
SAP UI5 应用开发教程之七十二 - SAP UI5 页面路由的动画效果设置