当前位置:网站首页>JS format current time and date calculation
JS format current time and date calculation
2022-04-23 07:03:00 【NOyesNONO_】
// The first one means that it is still a few days away from today , Such as 0 Today ,1 For tomorrow ,-1 Yesterday
// The second digit indicates the specified date , Default not to today , Support formats '2020-10-10', '2020/01/01'
function getTargetDate(num = 0, baseDate = null) {
let tarDate = ''// Target date
if (baseDate) {
tarDate = new Date(baseDate);
} else {
tarDate = new Date();
}
// If num Not for 0 Explain the passage of time , The base time needs to be converted into the form of timestamp for calculation
if (num !== 0) {
tarDate = tarDate.getFullYear() + '-' + (tarDate.getMonth() + 1) + '-' + tarDate.getDate() + ' 00:00:00';// Year-Month-Date 00:00:00
tarDate = Date.parse(tarDate.replace(/-/g, '/'));// Converts the specified time to a timestamp ,ie I won't support it ‘/’,Date.parse(Year-Month-Date)
tarDate += (86400000) * num;// Modified timestamp ,24*60*60 = 86400000 Milliseconds of the day
tarDate = new Date(tarDate);// Time stamp converted to time
// tarDate = new Date(tarDate.setDate(tarDate.getDate() + num))//todo This line replaces the four lines above
}
// hold new Date() In the form of format
const weekday = [
" Sunday ",
" Monday ",
" Tuesday ",
" Wednesday ",
" Thursday ",
" Friday ",
" Saturday ",
];
const y = tarDate.getFullYear();
let month = (tarDate.getMonth() + 1);
month = month < 10 ? "0" + month : month
let date = tarDate.getDate(); // direct date Plus or minus, there will be problems when you meet people across the moon ,getDate Get a number after ,01-2 = -1
date = date < 10 ? "0" + date : date
const d = weekday[tarDate.getDay()];
let h;
h = tarDate.getHours() < 10 ? "0" + tarDate.getHours() : tarDate.getHours()
let m;
m = tarDate.getMinutes() < 10 ? "0" + tarDate.getMinutes() : tarDate.getMinutes()
let s;
s = tarDate.getSeconds() < 10 ? "0" + tarDate.getSeconds() : tarDate.getSeconds()
let resDate = `${
y}-${
month}-${
date}`
let resNumDate = '' + y + month + date
let currentTime = `${
h}:${
m}:${
s}`
return {
year: y,
month: month,
date: date,
weekday: d,
hour: h,
minute: m,
second: s,
resDate: resDate,//Year-Month-Date
resNumDate: resNumDate,//YearMonthDte
currentTime: currentTime,//xx:xx:xx
}
}
// Supported input parameters
getTargetDate().currentTime // 16:05:05
getTargetDate().resNumDate //20201212 same day
getTargetDate().resDate//2020-12-12 same day
getTargetDate(-1).resDate//2020-12-11 A day earlier
getTargetDate().weekday // Saturday
getTargetDate(2,'2022-01-18').resDate //2022-01-20, Appoint 2022-01-18 Two days later
版权声明
本文为[NOyesNONO_]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230601165185.html
边栏推荐
猜你喜欢

LeetCode刷题|368最大整除子集(动态规划)

redis 常见问题

High performance gateway for interconnection between VPC and IDC based on dpdk

Prometheus Cortex多租户读写的实现
![[ES6 quick start]](/img/9e/4c4be5907c1f7b3485c2f4178b9150.png)
[ES6 quick start]

MySQL【ACID+隔离级别+ redo log + undo log】

SQL学习|复杂查询

Binary sum of leetcode questions

冬季实战营动手实战-上云必备环境准备,动手实操快速搭建LAMP环境 领鼠标 云小宝 背包 无影

qs.stringify 接口里把入参转为&连接的字符串(配合application/x-www-form-urlencoded请求头)
随机推荐
Oracle数据库性能分析之常用视图
ovs与ovs+dpdk架构分析
LeetCode刷题|38外观数组
阿里云日志服务sls的典型应用场景
异常记录-9
memcached 源码分析
基于ECS搭建云上博客(云小宝码上送祝福,免费抽iphone13任务详解)
Curry realization of function continuous call calculation and accumulation
【OSS文件上传快速入门】
Typescript (top)
[OSS file upload quick start]
重启Oracle监听器会中断已有连接吗
【Shell脚本练习】将新加的磁盘批量添加到指定的VG中
[fish in the net] ansible awx calls playbook to transfer parameters
通过源码探究@ModelAndView如何实现数据与页面的转发
Common views of Oracle database performance analysis
JS regular matching first assertion and last assertion
Kids and COVID: why young immune systems are still on top
rdma网络介绍
异常记录-7