当前位置:网站首页>【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)))
边栏推荐
- Tencent cloud webshell experience
- Deep learning environment configuration jupyter notebook
- 随时随地查看远程试验数据与记录——IPEhub2与IPEmotion APP
- Building a dream in the digital era, the Xi'an station of the city chain science and Technology Strategy Summit ended smoothly
- Attention slam: a visual monocular slam that learns from human attention
- C9高校,博士生一作发Nature!
- SuperSocket 1.6 创建一个简易的报文长度在头部的Socket服务器
- paddlehub应用出现paddle包报错的问题
- Slow database query optimization
- Activereportsjs 3.1 Chinese version | | | activereportsjs 3.1 English version
猜你喜欢

ESP Arduino (IV) PWM waveform control output

Deep understanding of distributed cache design

threejs图片变形放大全屏动画js特效

Threejs image deformation enlarge full screen animation JS special effect
![[software reverse - solve flag] memory acquisition, inverse transformation operation, linear transformation, constraint solving](/img/16/71692f4cf89b7dc0fe62946e59ecd1.png)
[software reverse - solve flag] memory acquisition, inverse transformation operation, linear transformation, constraint solving
![[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](/img/a5/41d4cbc070d421093323dc189a05cf.png)
[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

批量获取中国所有行政区域经边界纬度坐标(到县区级别)

第七篇,STM32串口通信编程

Stm32f407 ------- DAC digital to analog conversion

第四篇,STM32中断控制编程
随机推荐
[牛客] [NOIP2015]跳石头
Service asynchronous communication
第七篇,STM32串口通信编程
省市区三级坐标边界数据csv转JSON
【JVM调优实战100例】05——方法区调优实战(下)
C9 colleges and universities, doctoral students make a statement of nature!
一行代码实现地址信息解析
Return to blowing marshland -- travel notes of zhailidong, founder of duanzhitang
Zabbix 5.0:通过LLD方式自动化监控阿里云RDS
[Batch dos - cmd Command - Summary and Summary] - String search, find, Filter Commands (FIND, findstr), differentiation and Analysis of Find and findstr
【JokerのZYNQ7020】AXI_ EMC。
How do novices get started and learn PostgreSQL?
Eventbus source code analysis
什么是时间
界面控件DevExpress WinForms皮肤编辑器的这个补丁,你了解了吗?
在jupyter中实现实时协同是一种什么体验
Leetcode (547) - number of provinces
学习光线跟踪一样的自3D表征Ego3RT
C Primer Plus Chapter 14 (structure and other data forms)
from .cv2 import * ImportError: libGL.so.1: cannot open shared object file: No such file or direc