当前位置:网站首页>【js】获取当前时间的前后n天或前后n个月(时分秒年月日都可)
【js】获取当前时间的前后n天或前后n个月(时分秒年月日都可)
2022-07-06 17:18:00 【冰冷的希望】
方法由很多,但原理都是先获取当前时间再加减时间差
1.加减时间戳
new Date()时传入一个加减处理之后的时间戳即可
let nowDate = new Date();
// 3秒钟前
let nextDate = new Date(nowDate.getTime() - 3 * 1000);
// 3分钟前
let nextDate = new Date(nowDate.getTime() - 3 * 60 * 1000);
// 24小时毫秒数
let dayStamp = 24 * 60 * 60 * 1000
// 前1天
let preDate = new Date(nowDate.getTime() - dayStamp);
// 后3天
let nextDate = new Date(nowDate.getTime() + 3 * dayStamp);
2.api设置
Date对象有很多get和set方法分别获取和设置时分秒年月日
// 3秒钟前
let choiceStartDt = new Date(new Date().setSeconds((new Date().getSeconds() - 3)))
// 7秒钟后
let choiceStartDt = new Date(new Date().setSeconds((new Date().getSeconds() + 7)))
// 3分钟前
let choiceStartDt = new Date(new Date().setMinutes((new Date().getMinutes() - 3)))
// 3个小时前
let choiceStartDt = new Date(new Date().setHours((new Date().getHours() - 3)))
// 前1天
let choiceStartDt = new Date(new Date().setDate((new Date().getDate() - 1)))
// 前1个月
let choiceStartDt = new Date(new Date().setMonth((new Date().getMonth() - 1)))
// 前3年
let choiceStartDt = new Date(new Date().setFullYear((new Date().getFullYear() - 3)))
边栏推荐
- Openjudge noi 1.7 08: character substitution
- C Primer Plus Chapter 14 (structure and other data forms)
- threejs图片变形放大全屏动画js特效
- 腾讯云 WebShell 体验
- Telerik UI 2022 R2 SP1 Retail-Not Crack
- [batch dos-cmd command - summary and summary] - string search, search, and filter commands (find, findstr), and the difference and discrimination between find and findstr
- Cause of handler memory leak
- Part VI, STM32 pulse width modulation (PWM) programming
- Anfulai embedded weekly report no. 272: 2022.06.27--2022.07.03
- Data sharing of the 835 postgraduate entrance examination of software engineering in Hainan University in 23
猜你喜欢
![[100 cases of JVM tuning practice] 04 - Method area tuning practice (Part 1)](/img/7a/bd03943c39d3f731afb51fe2e0f898.png)
[100 cases of JVM tuning practice] 04 - Method area tuning practice (Part 1)

深度学习之线性代数

Come on, don't spread it out. Fashion cloud secretly takes you to collect "cloud" wool, and then secretly builds a personal website to be the king of scrolls, hehe

Deep understanding of distributed cache design
![[user defined type] structure, union, enumeration](/img/a5/d6bcfb128ff6c64f9d18ac4c209210.jpg)
[user defined type] structure, union, enumeration

集合(泛型 & List & Set & 自定义排序)

pytorch之数据类型tensor

windows安装mysql8(5分钟)
做微服务研发工程师的一年来的总结
![Explain in detail the matrix normalization function normalize() of OpenCV [norm or value range of the scoped matrix (normalization)], and attach norm_ Example code in the case of minmax](/img/87/3fee9e6f687b0c3efe7208a25f07f1.png)
Explain in detail the matrix normalization function normalize() of OpenCV [norm or value range of the scoped matrix (normalization)], and attach norm_ Example code in the case of minmax
随机推荐
pyflink的安装和测试
【JVM调优实战100例】05——方法区调优实战(下)
Address information parsing in one line of code
Zabbix 5.0:通过LLD方式自动化监控阿里云RDS
通过串口实现printf函数,中断实现串口数据接收
String comparison in batch file - string comparison in batch file
[batch dos-cmd command - summary and summary] - jump, cycle, condition commands (goto, errorlevel, if, for [read, segment, extract string]), CMD command error summary, CMD error
fastDFS数据迁移操作记录
ESP Arduino (IV) PWM waveform control output
Cause of handler memory leak
Meet the level 3 requirements of ISO 2.0 with the level B construction standard of computer room | hybrid cloud infrastructure
随时随地查看远程试验数据与记录——IPEhub2与IPEmotion APP
What is time
城联优品入股浩柏国际进军国际资本市场,已完成第一步
Slow database query optimization
Make a simple graphical interface with Tkinter
【批处理DOS-CMD命令-汇总和小结】-字符串搜索、查找、筛选命令(find、findstr),Find和findstr的区别和辨析
Part V: STM32 system timer and general timer programming
from . cv2 import * ImportError: libGL. so. 1: cannot open shared object file: No such file or direc
Configuring OSPF basic functions for Huawei devices