补充工具类

This commit is contained in:
xiaozzzi 2023-11-17 15:58:41 +08:00
parent 8bfef9d1f8
commit 2e6df9491b
2 changed files with 50 additions and 10 deletions

View File

@ -8,26 +8,25 @@ export const isNull = (val: any): boolean => {
// 控制
if (val == null || val === 'undefined' || val === undefined || val === '') {
return true;
return true
}
// 数组
if (Array.isArray(val) && val.length === 0) {
return true;
return true
}
// 对象, 但无字段
if (val instanceof Object && JSON.stringify(val) === '{}') {
return true;
return true
}
return false;
return false
}
export const isNotNull = (val: any): boolean => {
return !isNull(val);
return !isNull(val)
}
export const isBlank = (str: string | undefined | null): boolean => {
if (str === undefined) {
return true
@ -57,8 +56,8 @@ export const isEmpty = (val: any): boolean => {
}
if (val.length == 0) {
return true;
return true
}
return false;
}
return false
}

View File

@ -54,7 +54,48 @@ export const timestampToDatetime = (timestamp: number | string | Date): string =
return '' + y + '-' + m + '-' + d + ' ' + h + ':' + min + ':' + s + '.' + SSS
}
const formatNum = (num: string | number) => {
/**
* yyyy-MM-dd HH:mm:ss
* @returns {string}
*/
export const getDateTimeFormat = (): string => {
const now = new Date()
let y = now.getFullYear()
let m = formatNum(now.getMonth() + 1)
let d = formatNum(now.getDate())
let h = formatNum(now.getHours())
let min = formatNum(now.getMinutes())
let s = formatNum(now.getSeconds())
return '' + y + '-' + m + '-' + d + ' ' + h + ':' + min + ':' + s
}
/**
*
* @param date
* @param next
* @param format
* @returns
*/
export const getNextDay = (date: string, next: number = 1, format = '{y}-{m}-{d}'): string => {
if (!date) {
return '日期错误'
}
date = date.match(/\d+/g)!.join('-') // 格式为2022年09月19日处理
const nextDay = new Date(date)
nextDay.setDate(nextDay.getDate() + next)
const formatObj: any = {
y: nextDay.getFullYear(),
m: nextDay.getMonth() + 1,
d: nextDay.getDate()
}
return format.replace(/{([ymd])+}/g, (_result, key) => {
const value = formatObj[key]
return value.toString().padStart(2, '0')
})
}
const formatNum = (num: number) => {
if (num < 10) {
return '0' + num
}