当前位置:网站首页>【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)))
边栏推荐
- Eventbus source code analysis
- ActiveReportsJS 3.1中文版|||ActiveReportsJS 3.1英文版
- How do novices get started and learn PostgreSQL?
- Linear algebra of deep learning
- Meet the level 3 requirements of ISO 2.0 with the level B construction standard of computer room | hybrid cloud infrastructure
- 在jupyter中实现实时协同是一种什么体验
- [C language] dynamic address book
- Building a dream in the digital era, the Xi'an station of the city chain science and Technology Strategy Summit ended smoothly
- Niuke cold training camp 6B (Freund has no green name level)
- 代码克隆的优缺点
猜你喜欢
Niuke cold training camp 6B (Freund has no green name level)
Five different code similarity detection and the development trend of code similarity detection
Zynq transplant ucosiii
做微服务研发工程师的一年来的总结
深度学习之数据处理
Configuring OSPF basic functions for Huawei devices
Periodic flash screen failure of Dell notebook
深度学习之线性代数
【批处理DOS-CMD命令-汇总和小结】-跳转、循环、条件命令(goto、errorlevel、if、for[读取、切分、提取字符串]、)cmd命令错误汇总,cmd错误
Building a dream in the digital era, the Xi'an station of the city chain science and Technology Strategy Summit ended smoothly
随机推荐
深度学习框架TF安装
通过串口实现printf函数,中断实现串口数据接收
Tencent cloud webshell experience
Slow database query optimization
重上吹麻滩——段芝堂创始人翟立冬游记
5种不同的代码相似性检测,以及代码相似性检测的发展趋势
Zabbix 5.0:通过LLD方式自动化监控阿里云RDS
UI控件Telerik UI for WinForms新主题——VS2022启发式主题
Dell筆記本周期性閃屏故障
Part IV: STM32 interrupt control programming
Part VI, STM32 pulse width modulation (PWM) programming
[C language] dynamic address book
JS+SVG爱心扩散动画js特效
Slam d'attention: un slam visuel monoculaire appris de l'attention humaine
迈动互联中标北京人寿保险,助推客户提升品牌价值
第四篇,STM32中断控制编程
There is an error in the paddehub application
Rainstorm effect in levels - ue5
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
做微服务研发工程师的一年来的总结