当前位置:网站首页>Time formatting
Time formatting
2022-07-25 02:57:00 【Cicadas haven't slept yet】
1、 Time format
export const formatDate = (date, fmt) => {
date = date || new Date();
fmt = fmt || 'yyyy-MM-dd hh:mm:ss';
if (Object.prototype.toString.call(date).slice(8, -1) !== 'Date') {
date = new Date(date)
}
if (/(y+)/.test(fmt)) {
fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
}
let o = {
'M+': date.getMonth() + 1,
'd+': date.getDate(),
'h+': date.getHours(),
'm+': date.getMinutes(),
's+': date.getSeconds()
}
for (let k in o) {
if (new RegExp(`(${k})`).test(fmt)) {
let str = o[k] + ''
fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? str : (("00" +
o[k]).substr(("" + o[k]).length)))
}
}
return fmt
}2、 Get the current day 、 month 、 Year specific time
/**
* Get current day 、 month 、 Year start time
* @param {*} type Time type
*/
export function getCurrStartTime(type) {
let time
if (type == "day") {
time = formatDate(new Date(new Date().getTime()), "yyyy-MM-dd") + " 00:00:00"
} else if (type == "mouth") {
time = formatDate(new Date(new Date().getTime()), "yyyy-MM") + "-01 00:00:00"
} else {
time = formatDate(new Date(new Date().getTime()), "yyyy") + "-01-01 00:00:00"
}
return time
}
/**
* Get current day 、 month 、 Year end time
* @param {*} type Time type
*/
export function getCurrEndTime(type) {
let time
if (type == "day") {
time = formatDate(new Date(new Date().getTime() + 24 * 60 * 60 * 1000), "yyyy-MM-dd") + " 00:00:00"
} else if (type == "mouth") {
let item = new Date().setMonth(new Date().getMonth() + 1);
time = formatDate(new Date(item), "yyyy-MM") + "-01 00:00:00"
} else {
time = String(Number(formatDate(new Date(new Date().getTime()), "yyyy")) + 1) + "-01-01 00:00:00"
}
return time
}3、 Get the time of the same period according to the incoming time , Yesterday, 、 Last month 、 last year
/**
* Get the time of the same period , Yesterday, , Last month , last year
* @param {*} checktime Start time of query
* @param {*} timetype Japan 、 month 、 year
*/
export function getCheckBeforeTime(checktime,timetype){
let newtiem={}
if(timetype=="day"){
newtiem.fromDate = formatDate(new Date(new Date(checktime).getTime()-24*60*60*1000), "yyyy-MM-dd") + " 00:00:00"
}else if(timetype=="month"){
let item = new Date(checktime).setMonth(new Date(checktime).getMonth() - 1);
let time = formatDate(new Date(item), "yyyy-MM") + "-01 00:00:00"
newtiem.fromDate = time
}else{
let time = String(Number(formatDate(new Date(new Date(checktime).getTime()), "yyyy")) - 1) + "-01-01 00:00:00"
newtiem.fromDate = time
}
newtiem.toDate = checktime
return newtiem
}4、 According to the incoming time , Get today of last year 、 This month last year
/**
* Get the cycle time
* @param {*} checktime_form // Incoming start time
* @param {*} checktime_to // End time of incoming
* @param {*} timetype // Time type
*/
export function getCheckRepeatTime(checktime_form,checktime_to,timetype){ // Get the time of the same period , Yesterday, , Last month , last year
let newtiem={}
let item = new Date(checktime_form).setFullYear((new Date(checktime_form).getFullYear()-1))
let item_to = new Date(checktime_to).setFullYear((new Date(checktime_to).getFullYear()-1))
let time_from='',time_to=''
if(timetype=="day"){
time_from = formatDate(new Date(item), "yyyy-MM-dd") + " 00:00:00"
time_to = formatDate(new Date(item_to), "yyyy-MM-dd") + " 00:00:00"
}else if(timetype=="month"){
time_from = formatDate(new Date(item), "yyyy-MM") + "-01 00:00:00"
time_to = formatDate(new Date(item_to), "yyyy-MM") + "-01 00:00:00"
}else{
time_from = formatDate(new Date(item), "yyyy") + "-01-01 00:00:00"
time_to = formatDate(new Date(item_to), "yyyy") + "-01-01 00:00:00"
}
newtiem.fromDate = time_from
newtiem.toDate = time_to
return newtiem
}Regular summary :
cell-phone number :
let zz = /^1\d{10}$/.test("13800138000") // return true It is
6 Digit verification code :
/^\d{6}$/.test(this.yznum)According to the incoming days , Get the date a few days ago
// obtain num The time and date before , //num Is a positive number indicating the time after ,num Negative numbers indicate the time before ,0 Today
fun_date(num){
var date1 = new Date();
var date2 = new Date(date1);
date2.setDate(date1.getDate() + num);
var time2 = date2.getFullYear() + "-" + (date2.getMonth() + 1) + "-" + date2.getDate();
return time2;
}
边栏推荐
- "Introduction to interface testing" punch in to learn day05: how can a testing framework support restful interfaces?
- Object.defineproperty use
- Solve ''_ Xsrf 'argument missing from post
- Concurrent programming day01
- Rotating frame target detection mmrotate v0.3.1 training hrsc2016 data set (II)
- Tp5.1 initialize initialization method (not \u initialize)
- Mgre.hdlc.ppp.chap.nat comprehensive experiment
- Download the jar package of jsqlparser and PageHelper
- Banana pie bpi-m5 toss record (3) -- compile BSP
- Solve the error: could not find 'xxxtest‘
猜你喜欢

After working for two months in the summer vacation, I understood three routing schemes of keepalived high availability

JS written test question -- realize the flat function of array

Publish the project online and don't want to open a port
![[pyGame practice] nostalgic classic - do you remember the name of this chess game for children? (must collect)](/img/b3/075ad2d555118272efede5a9969319.png)
[pyGame practice] nostalgic classic - do you remember the name of this chess game for children? (must collect)

Tp5.1 paging (with parameter transfer)

English grammar_ Reflexive pronoun
![[stm32f103rct6] can communication](/img/24/71509bd0d74d43ce4a79b8126478ff.jpg)
[stm32f103rct6] can communication

Tensorflow's study notes (I)

Beginners must see the markdown User Guide

@Retryable @backoff @recover retry the use of annotations
随机推荐
Keil compile download error: no algorithm found for: 08000000h - 08001233h solution
Banana pie bpi-m5 toss record (2) -- compile u-boot
Mgre.hdlc.ppp.chap.nat comprehensive experiment
Technical experts from large factories: Thoughts on cloud nativity and software supply chain security
New key points of ES6
Sequence diagram of UML diagram series
# CF #808 Div.2(A - C)
Daily three questions 7.16
Get to know string thoroughly
Domain driven model (DDD)
What should I do when the interface encounters jsonstring
Keras load history H5 format model error: attributeerror 'STR' object has no attribute 'decode‘
JS foundation -- regular expression
Flume's study notes
【C】 Advanced knowledge of file operation
Insertion of balanced binary tree (AVL tree)
[jailhouse article] scheduling policies and system software architectures for mixed criticality
File file name change
Tp5.1 paging (with parameter transfer)
Picgo configuring Alibaba cloud OSS