当前位置:网站首页>JS date and time format (year, month, day, hour, minute, second, week, quarter, time difference acquisition, date and timestamp conversion function)
JS date and time format (year, month, day, hour, minute, second, week, quarter, time difference acquisition, date and timestamp conversion function)
2022-07-27 12:38:00 【ray_ zzzzz】
Encapsulates a tool class for date formatting
The notes are very clear
(function(window){
window.pub = {
// Format date
dateFormat:function(format,date){
/* * If there is a name in the document dateFormat, You can use dFormat() call * year (Y) You can use 1-4 Placeholders * month (m)、 Japan (d)、 Hours (H)、 branch (M)、 second (S) You can use 1-2 Placeholders * week (W) You can use 1-3 Placeholders * quarter (q For Arabic numerals ,Q Chinese numbers ) You can use 1 or 4 Placeholders * * let date = new Date() * dateFormat("YYYY-mm-dd HH:MM:SS", date) 2020-02-09 14:04:23 * dateFormat("YYYY-mm-dd HH:MM:SS Q", date) 2020-02-09 14:09:03 One * dateFormat("YYYY-mm-dd HH:MM:SS WWW", date) 2020-02-09 14:45:12 Sunday * dateFormat("YYYY-mm-dd HH:MM:SS QQQQ", date) 2020-02-09 14:09:36 first quarter * dateFormat("YYYY-mm-dd HH:MM:SS WWW QQQQ", date) 2020-02-09 14:46:12 Sunday first quarter */
let we = date.getDay(); // week
let qut = Math.floor((date.getMonth()+3)/3).toString(); // quarter
const opt = {
"Y+":date.getFullYear().toString(), // year
"m+":(date.getMonth()+1).toString(), // month ( Month from 0 Start , want +1)
"d+":date.getDate().toString(), // Japan
"H+":date.getHours().toString(), // when
"M+":date.getMinutes().toString(), // branch
"S+":date.getSeconds().toString(), // second
"q+":qut, // quarter
};
const week = {
// Chinese numbers ( week )
"0":" Japan ",
"1":" One ",
"2":" Two ",
"3":" 3、 ... and ",
"4":" Four ",
"5":" 5、 ... and ",
"6":" 6、 ... and "
};
const quarter = {
// Chinese numbers ( quarter )
"1" : " One ",
"2" : " Two ",
"3" : " 3、 ... and ",
"4" : " Four ",
};
if(/(W+)/.test(format)){
format = format.replace(RegExp.$1,(RegExp.$1.length >1 ? (RegExp.$1.length >2 ? ' week '+week[we] : ' Zhou '+week[we]) : week[we]))
};
if (/(Q+)/.test(format)) {
// Enter a Q, Only one Chinese number is output , Input 4 individual Q, Then concatenate the string
format = format.replace(RegExp.$1,(RegExp.$1.length == 4 ? ' The first '+quarter[qut]+' quarter ' : quarter[qut]));
};
for(let k in opt){
let r = new RegExp("("+k+")").exec(format);
if(r){
// If the input length is not 1, Then fill in zero before
format = format.replace(r[1],(RegExp.$1.length == 1 ? opt[k] : opt[k].padStart(RegExp.$1.length,'0')))
}
};
return format;
},
// Time difference
// Small in front and big in back
getTimeBetween:function(dateString1,dateString2){
let TimeBetween = {
};
let startDate = Date.parse(dateString1);
let endDate = Date.parse(dateString2);
// Days
TimeBetween.days = (endDate - startDate)/(1*24*60*60*1000);
// Hours
TimeBetween.hours = TimeBetween.days * 24;
// minute
TimeBetween.mins = TimeBetween.days * 24 * 60;
// Number of seconds
TimeBetween.sec = TimeBetween.days * 24*60*60;
return TimeBetween;
},
// Format time difference
formatTB:function(dateString1,dateString2){
let date = getTimeBetween(dateString1,dateString2);
date.days = date.sec/86400;
date.hours = date.sec%86400/3600;
date.mins = date.sec%86400%3600/60;
return date;
},
// Timestamp conversion
getTimeStamp:function(date){
let TStamp = {
};
// Date to time stamp
TStamp.timeStamp = Date.parse(date);
// Time stamp date
TStamp.dateTime = new Date(date); // You can use the above dateFormat Format time
return TStamp;
}
}
})(window)
边栏推荐
- J9 number theory: how long is the mainstreaming of decentralized identity?
- 2021-3-22-tencent - minimum number of guards
- You haven't connected to the proxy server. There may be a problem or the address is incorrect (how to check the proxy server IP)
- Go Beginner (3)
- Bishi journey
- 20210422 consolidation interval
- Go Beginner (5)
- ArrayList常用方法总结
- 详述jdbc查询方法执行过程
- Chapter 13 IO flow
猜你喜欢

Multi activity disaster recovery construction after station B 713 accident | takintalks share
Several rounds of SQL queries in a database

Image segmentation vs Adobe Photoshop (PS)

概述有名内部类与匿名内部类

Solution: the idea project does not display a tree view

Interviewer: how can you close an order without using a scheduled task?

Self built personalized automatic quotation system to cope with changeable quotation mode

How to use the server to build our blog

详述throw与throws

JVM memory layout detailed, illustrated, well written!
随机推荐
Configuration files in MySQL
Transactions in MySQL
Basic architecture of data Lake
How to use the server to build our blog
2021-4-7-find the maximum water holding capacity
Insert sort summary
Plus版SBOM:流水线物料清单PBOM
Does the service layer need an interface
20210512 recursive formula
II. Analysis of the execution process of make menuconfig
虚拟偶像的歌声原来是这样生成的!
Implicit indicators for evaluating the advantages and disadvantages of automated testing
Openpyxl drawing area map
Unity 2D game tutorial
C program debugging and exception handling (try catch)
记忆中的香河肉饼
Bishi journey
多表查询
详述反射中构造方法、属性和普通方法
An overview of kernel compilation system