当前位置:网站首页>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
边栏推荐
- 100000 college students have become ape powder. What are you waiting for?
- Example interview | sun Guanghao: College Club grows and starts a business with me
- 2021年6月程序员工资统计,平均15052元,你拖后腿了吗?
- [walking notes]
- SHA512 / 384 principle and C language implementation (with source code)
- Vscode tips
- Stack protector under armcc / GCC
- MySQL5. 5 installation tutorial
- Lpddr4 notes
- CSDN College Club "famous teacher college trip" -- Hunan Normal University Station
猜你喜欢
[indicators] precision, recall
缘结西安 | CSDN与西安思源学院签约,全面开启IT人才培养新篇章
vscode小技巧
Short name of common UI control
联想拯救者Y9000X 2020
切线空间(tangent space)
【官宣】长沙软件人才实训基地成立!
Logstash数据处理服务的输入插件Input常见类型以及基本使用
[point cloud series] learning representations and generative models for 3D point clouds
Common interview questions and detailed analysis of the latest Android developers in 2020
随机推荐
innobackupex增量备份
You and the 42W bonus pool are one short of the "Changsha bank Cup" Tencent yunqi innovation competition!
LeetCode_ DFS_ Medium_ 695. Maximum area of the island
Detailed explanation of ADB shell top command
你和42W奖金池,就差一次“长沙银行杯”腾讯云启创新大赛!
web三大组件之Filter、Listener
Logstash数据处理服务的输入插件Input常见类型以及基本使用
Riscv MMU overview
kettle庖丁解牛第16篇之输入组件周边讲解
Isparta is a tool that generates webp, GIF and apng from PNG and supports the transformation of webp, GIF and apng
MySQL5. 5 installation tutorial
面试官给我挖坑:单台服务器并发TCP连接数到底可以有多少 ?
What does the SQL name mean
[official announcement] Changsha software talent training base was established!
XML
TERSUS笔记员工信息516-Mysql查询(2个字段的时间段唯一性判断)
Longitude and latitude position of provincial capitals in China
Armv8m (cortex M33) MPU actual combat
Analysis of the latest Android high frequency interview questions in 2020 (BAT TMD JD Xiaomi)
集简云 x 飞书深诺,助力企业运营部实现自动化办公