You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

45 lines
1.3 KiB

/**
* 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.Dayjs | undefined = undefined,
format = DATE_TIME_FORMAT,
): string {
return dayjs(date).format(format)
}
export function formatToDate(
date: dayjs.Dayjs | undefined = undefined,
format = DATE_FORMAT,
): string {
return dayjs(date).format(format)
}
export const dateUtil = dayjs
/**
* 起始日期到结束日期的时间间隔
* @param startDate 起始日期
* @param endDate 结束日期
*/
export function shareDataTime(startDate: Date, endDate: Date) {
const timeOld = endDate.getTime() - startDate.getTime() //总豪秒数
const eDaysOld = timeOld / (24 * 60 * 60 * 1000)
const daysOld = Math.floor(eDaysOld) //相差天数
const eHoursOld = (eDaysOld - daysOld) * 24
const hoursOld = Math.floor(eHoursOld) //相差小时数
const eMinutesOld = (eHoursOld - hoursOld) * 60
const minutesOld = Math.floor(eMinutesOld) //相差分钟数
const seconds = Math.floor((eMinutesOld - minutesOld) * 60) //相差秒数
return {
days: daysOld,
hours: hoursOld,
minutes: minutesOld,
seconds,
}
}