当前位置:网站首页>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)
边栏推荐
- Solution: the idea project does not display a tree view
- PySide6/PyQt开发经验总结(2) - 设置快捷键
- SQL question brushing: find out the current salary of all employees
- The strongest distributed locking tool: redisson
- Map接口
- Photoshop web design tutorial
- Uniapp video video playback is not completed. It is forbidden to drag the progress bar fast forward
- XXL job parameter transfer
- NFT mall /nft blind box / virtual blind box /nft transaction / customizable second opening
- Plus版SBOM:流水线物料清单PBOM
猜你喜欢

Xposed+FDex2 app脱壳 (黑猫投诉app脱壳)

HDU1698_Just a Hook

Finally, I was ranked first in the content ranking in the professional field. I haven't been tired in vain during this period. Thanks to CSDN's official platform, I'm lucky and bitter.

The sparksubmit. Main () method submits external parameters and remotely submits the standalone cluster task

Fundamentals of mathematics 02 - sequence limit

Necessary foundation: Signature Verification

II. Analysis of the execution process of make menuconfig

CMD Chinese garbled code solution

Image segmentation vs Adobe Photoshop (PS)

ArrayList常用方法总结
随机推荐
nodejs body-parser中间件处理类型 multipart/form-data 的 POST 表单数据,req.body无法接收到数据
Set接口
Plus SBOM: assembly line BOM pbom
HDU1698_ Just a Hook
ArrayList常用方法总结
Does the service layer need an interface
P1876 turn on the light [getting started]
20210419 combined sum
20210519 leetcode double pointer
SSM实战项目-前后分离(简单易懂)
详述throw与throws
Security measures for tcp/ip protocol vulnerabilities
12 pictures, take you to thoroughly understand ZGC garbage collector!
Chapter 10 enumeration classes and annotations
BSP video tutorial issue 21: easy one key implementation of serial port DMA variable length transceiver, support bare metal and RTOS, including MDK and IAR, which is more convenient than stm32cubemx (
Bishi journey
QT | qcheckbox of control
Laboratory procedures and references of chloramphenicol acetate
Xposed+FDex2 app脱壳 (黑猫投诉app脱壳)
US pressure surges tiktok changes global safety director