当前位置:网站首页>【js】获取当前时间的前后n天或前后n个月(时分秒年月日都可)
【js】获取当前时间的前后n天或前后n个月(时分秒年月日都可)
2022-08-10 23:50: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)))
边栏推荐
- PMP每日一练 | 考试不迷路-8.10(包含敏捷+多选)
- Mysql.慢Sql
- 给肯德基打工的调料商,年赚两亿
- Design and implementation of flower online sales management system
- Pagoda Test-Building PHP Online Mock Exam System
- YOLOv5的Tricks | 【Trick11】在线模型训练可视化工具wandb(Weights & Biases)
- SQL注入基础---order by \ limit \ 宽字节注入
- Dump文件生成,内容,以及分析
- iNFTnews | In the Web3 era, users will have data autonomy
- [C language articles] Expression evaluation (implicit type conversion, arithmetic conversion)
猜你喜欢
阿里P7晒出1月工资单:狠补了这个,真香...
报错:Client does not support authentication protocol requested by server; consider upgrading MySQL cli
Design and implementation of flower online sales management system
【C语言】C语言程序设计:动态通讯录(顺序表实现)
3. 容器功能
YOLOv5的Tricks | 【Trick13】YOLOv5的detect.py脚本的解析与简化
Which foreign language journals and conferences can be submitted for software engineering/system software/programming language?
sqlmap结合dnslog快速注入
[Excel知识技能] 将文本型数字转换为数值格式
[Excel知识技能] 将“假“日期转为“真“日期格式
随机推荐
Which translation software is more accurate [Free]
【pypdf2】安装、读取和保存、访问页面、获取文本、读写元数据、加密解密
App的回归测试,有什么高效的测试方法?
sqlmap combined with dnslog fast injection
如何判断一个数为多少进制?
翻译软件哪个准确度高【免费】
[Excel knowledge and skills] Convert numeric format numbers to text format
报错:Client does not support authentication protocol requested by server; consider upgrading MySQL cli
What is the ASIO4ALL
14. Thymeleaf
开启新征程——枫叶先生第一篇博客
Excel English automatic translation into Chinese tutorial
Qt入门(六)——抽奖系统的实现
图像识别和语义分割的区别
13. 内容协商
5. Lombok
[C language] binary search (half search)
How to quickly grasp industry opportunities and introduce new ones more efficiently is an important proposition
Dump file generation, content, and analysis
Three-column layout implementation