当前位置:网站首页>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;
}
边栏推荐
- Eslint error
- Preliminary foundation JVM
- Banana pie bpi-m5 toss record (3) -- compile BSP
- 6.0 cancellation of member registration verification code
- JS written test -- regular expression
- [jailhouse article] scheduling policies and system software architectures for mixed criticality
- JS written test question -- browser kernel
- Vite dynamically loads static resource pictures, and fixes the 404 problem of pictures after packaging.
- Selenium framework operation steelth.min.js file hides browser fingerprint features
- JS foundation -- math
猜你喜欢

Nuscenes data set summary

Tp5.1 paging (with parameter transfer)

Learning record 12

YouTube Download and (batch) Download

Redux best practices "Redux toolkit"

English grammar_ Reflexive pronoun

JS written test -- regular expression
![[stm32f103rct6] motor PWM drive module idea and code](/img/a5/3010acff73c8913e967ff3a024877e.png)
[stm32f103rct6] motor PWM drive module idea and code

Classic network learning RESNET code implementation

Pagoda workman WSS reverse proxy socket legal domain name applet chat remove port
随机推荐
Matlab draws radar chart (four lines of code)
Tp5.1 login configuration method of whether to login public functions (complete login case)
C language: Structure -- a detailed explanation of memory byte alignment
Review all frames before sum of SSM frames
Get to know string thoroughly
[jailhouse article] scheduling policies and system software architectures for mixed criticality
Mgre.hdlc.ppp.chap.nat comprehensive experiment
2022-07-19: all factors of F (I): I are added up after each factor is squared. For example, f (10) = 1 square + 2 square + 5 square + 10 square = 1 + 4 + 25 + 100 = 130.
Learning Record V
ICO objects in classification
DOM operation -- get elements and nodes
Resolve the error: org.apache.ibatis.binding.bindingexception
Threat report in June: new bank malware malibot poses a threat to mobile banking users
Classic network learning RESNET code implementation
The latest interview questions and analysis of software testing in 2022
Eslint error
MySQL common function summary, very practical, often encountered in interviews
JS foundation -- task queue and event loop
Define macros in makefile and pass them to source code
【C】 Advanced knowledge of file operation