当前位置:网站首页>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
边栏推荐
- [point cloud series] foldingnet: point cloud auto encoder via deep grid deformation
- Playwright controls local Google browsing to open and download files
- SAP ui5 application development tutorial 72 - animation effect setting of SAP ui5 page routing
- Longitude and latitude position of provincial capitals in China
- 你和42W奖金池,就差一次“长沙银行杯”腾讯云启创新大赛!
- 普通大学生如何拿到大厂offer?敖丙教你一招致胜!
- MySQL5.5安装教程
- SSM整合之pom.xml
- @Excellent you! CSDN College Club President Recruitment!
- FatFs FAT32 learning notes
猜你喜欢
SHA512 / 384 principle and C language implementation (with source code)
X509 parsing
2020年最新字节跳动Android开发者常见面试题及详细解析
How to build a line of code with M4 qprotex
Xi'an CSDN signed a contract with Xi'an Siyuan University, opening a new chapter in IT talent training
集简云 x 飞书深诺,助力企业运营部实现自动化办公
普通大学生如何拿到大厂offer?敖丙教你一招致胜!
交叉碳市场和 Web3 以实现再生变革
Example interview | sun Guanghao: College Club grows and starts a business with me
Campus takeout system - "nongzhibang" wechat native cloud development applet
随机推荐
CSDN College Club "famous teacher college trip" -- Hunan Normal University Station
Armv8m (cortex M33) MPU actual combat
[dynamic programming] 221 Largest Square
【快排】215. 数组中的第K个最大元素
Using open to open a file in JNI returns a - 1 problem
On the bug of JS regular test method
torch. Where can transfer gradient
这个SQL语名是什么意思
According to the salary statistics of programmers in June 2021, the average salary is 15052 yuan. Are you holding back?
vscode小技巧
SSM整合之pom.xml
十万大学生都已成为猿粉,你还在等什么?
Feature Engineering of interview summary
@优秀的你!CSDN高校俱乐部主席招募!
[quick platoon] 215 The kth largest element in the array
innobackupex增量备份
playwright控制本地穀歌瀏覽打開,並下載文件
Unified task distribution scheduling execution framework
Explanation of input components in Chapter 16
[point cloud series] multi view neural human rendering (NHR)