当前位置:网站首页>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
边栏推荐
猜你喜欢
Introduction to common APIs for EBFP programming
冬季实战营动手实战-上云必备环境准备,动手实操快速搭建LAMP环境 领鼠标 云小宝 背包 无影
Winter combat camp hands-on combat - first understand the cloud foundation, hands-on practice ECS ECS ECS novice on the road to get the mouse cloud Xiaobao backpack shadowless
rdma网络介绍
Redis FAQ
JS implementation of web page rotation map
Prometheus的relabel_configs和metric_relabel_configs解释及用法示例
【Lombok快速入门】
Construire un blog Cloud basé sur ECS (bénédiction sur le Code Cloud Xiaobao, explication détaillée de la tâche iphone13 gratuite)
qs.stringify 接口里把入参转为&连接的字符串(配合application/x-www-form-urlencoded请求头)
随机推荐
Kubernetes CoreDNS常见问题资料参考
Openvswitch compilation and installation
SQL学习|基础查询与排列
【Lombok快速入门】
Typescript (top)
[Lombok quick start]
How to use tiup to deploy a tidb V5 0 cluster
[fish in the net] ansible awx calls playbook to transfer parameters
Unix期末考试总结--针对直系
prometheus告警记录持久化(历史告警保存与统计)
try catch 不能捕获异步错误
Leetcode integer plus one
How to use DBA_ hist_ active_ sess_ History analysis database history performance problems
【漏网之鱼】Ansible AWX调用playbook传参问题
用Future与CountDownLatch实现多线程执行多个异步任务,任务全部完成后返回结果
Number of stair climbing methods of leetcode
DNA reveals surprise ancestry of mysterious Chinese mummies
ubuntu下搭建mysql环境 & 初识SQL
TC ebpf practice
异常记录-6