2023-03-18 13:10:54 +00:00
|
|
|
/**
|
|
|
|
* Independent time operation tool to facilitate subsequent switch to dayjs
|
|
|
|
*/
|
|
|
|
import dayjs from 'dayjs'
|
|
|
|
|
|
|
|
const DATE_TIME_FORMAT = 'YYYY-MM-DD HH:mm:ss'
|
|
|
|
const DATE_FORMAT = 'YYYY-MM-DD'
|
|
|
|
|
|
|
|
export function formatToDateTime(date?: dayjs.ConfigType, format = DATE_TIME_FORMAT): string {
|
|
|
|
return dayjs(date).format(format)
|
|
|
|
}
|
|
|
|
|
|
|
|
export function formatToDate(date?: dayjs.ConfigType, format = DATE_FORMAT): string {
|
|
|
|
return dayjs(date).format(format)
|
|
|
|
}
|
|
|
|
|
2023-03-30 14:53:24 +00:00
|
|
|
export function beginOfDay(date) {
|
|
|
|
return new Date(date.getFullYear(), date.getMonth(), date.getDate())
|
|
|
|
}
|
|
|
|
|
|
|
|
export function endOfDay(date) {
|
|
|
|
return new Date(date.getFullYear(), date.getMonth(), date.getDate(), 23, 59, 59, 999)
|
|
|
|
}
|
|
|
|
|
|
|
|
export function betweenDay(date1, date2) {
|
|
|
|
date1 = convertDate(date1)
|
|
|
|
date2 = convertDate(date2)
|
|
|
|
// 计算差值
|
|
|
|
return Math.floor((date2.getTime() - date1.getTime()) / (24 * 3600 * 1000))
|
|
|
|
}
|
|
|
|
|
|
|
|
export function formatDate(date, fmt) {
|
|
|
|
date = convertDate(date)
|
|
|
|
const o = {
|
|
|
|
'M+': date.getMonth() + 1, //月份
|
|
|
|
'd+': date.getDate(), //日
|
|
|
|
'H+': date.getHours(), //小时
|
|
|
|
'm+': date.getMinutes(), //分
|
|
|
|
's+': date.getSeconds(), //秒
|
|
|
|
'q+': Math.floor((date.getMonth() + 3) / 3), //季度
|
|
|
|
S: date.getMilliseconds() //毫秒
|
|
|
|
}
|
|
|
|
if (/(y+)/.test(fmt)) {
|
|
|
|
// 年份
|
|
|
|
fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
|
|
|
|
}
|
|
|
|
for (const k in o) {
|
|
|
|
if (new RegExp('(' + k + ')').test(fmt)) {
|
|
|
|
fmt = fmt.replace(RegExp.$1, RegExp.$1.length === 1 ? o[k] : ('00' + o[k]).substr(('' + o[k]).length))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return fmt
|
|
|
|
}
|
|
|
|
|
|
|
|
export function addTime(date, time) {
|
|
|
|
date = convertDate(date)
|
|
|
|
return new Date(date.getTime() + time)
|
|
|
|
}
|
|
|
|
|
|
|
|
export function convertDate(date) {
|
|
|
|
if (typeof date === 'string') {
|
|
|
|
return new Date(date)
|
|
|
|
}
|
|
|
|
return date
|
|
|
|
}
|
|
|
|
|
2023-03-18 13:10:54 +00:00
|
|
|
export const dateUtil = dayjs
|