当前位置:网站首页>[JS] obtain the N days before and after the current time or the n months before and after the current time (hour, minute, second, year, month, day)
[JS] obtain the N days before and after the current time or the n months before and after the current time (hour, minute, second, year, month, day)
2022-07-07 01:10:00 【Cold hope】
There are many ways , But the principle is to obtain the current time first and then add or subtract the time difference
1. Plus or minus timestamp
new Date() You can pass in a timestamp after addition and subtraction
let nowDate = new Date();
// 3 Seconds ago
let nextDate = new Date(nowDate.getTime() - 3 * 1000);
// 3 Minutes ago
let nextDate = new Date(nowDate.getTime() - 3 * 60 * 1000);
// 24 Hours milliseconds
let dayStamp = 24 * 60 * 60 * 1000
// front 1 God
let preDate = new Date(nowDate.getTime() - dayStamp);
// after 3 God
let nextDate = new Date(nowDate.getTime() + 3 * dayStamp);
2.api Set up
Date There are many objects get and set Methods get and set the hour, minute, second, year, day
// 3 Seconds ago
let choiceStartDt = new Date(new Date().setSeconds((new Date().getSeconds() - 3)))
// 7 Seconds later
let choiceStartDt = new Date(new Date().setSeconds((new Date().getSeconds() + 7)))
// 3 Minutes ago
let choiceStartDt = new Date(new Date().setMinutes((new Date().getMinutes() - 3)))
// 3 An hour ago
let choiceStartDt = new Date(new Date().setHours((new Date().getHours() - 3)))
// front 1 God
let choiceStartDt = new Date(new Date().setDate((new Date().getDate() - 1)))
// front 1 Months
let choiceStartDt = new Date(new Date().setMonth((new Date().getMonth() - 1)))
// front 3 year
let choiceStartDt = new Date(new Date().setFullYear((new Date().getFullYear() - 3)))
边栏推荐
猜你喜欢
Part 7: STM32 serial communication programming
ARM裸板调试之JTAG原理
JTAG debugging experience of arm bare board debugging
Configuring OSPF basic functions for Huawei devices
[HFCTF2020]BabyUpload session解析引擎
《安富莱嵌入式周报》第272期:2022.06.27--2022.07.03
Anfulai embedded weekly report no. 272: 2022.06.27--2022.07.03
[Batch dos - cmd Command - Summary and Summary] - String search, find, Filter Commands (FIND, findstr), differentiation and Analysis of Find and findstr
Dr selection of OSPF configuration for Huawei devices
ActiveReportsJS 3.1中文版|||ActiveReportsJS 3.1英文版
随机推荐
[Batch dos - cmd Command - Summary and Summary] - String search, find, Filter Commands (FIND, findstr), differentiation and Analysis of Find and findstr
boot - prometheus-push gateway 使用
[software reverse automation] complete collection of reverse tools
新手如何入门学习PostgreSQL?
Zabbix 5.0:通过LLD方式自动化监控阿里云RDS
What kind of experience is it to realize real-time collaboration in jupyter
windows安装mysql8(5分钟)
The printf function is realized through the serial port, and the serial port data reception is realized by interrupt
Dynamic planning idea "from getting started to giving up"
深入探索编译插桩技术(四、ASM 探秘)
通过串口实现printf函数,中断实现串口数据接收
Activereportsjs 3.1 Chinese version | | | activereportsjs 3.1 English version
A brief history of deep learning (II)
[100 cases of JVM tuning practice] 04 - Method area tuning practice (Part 1)
C9 colleges and universities, doctoral students make a statement of nature!
NEON优化:性能优化常见问题QA
【案例分享】网络环路检测基本功能配置
斗地主游戏的案例开发
NEON优化:log10函数的优化案例
Slow database query optimization