44 lines
1.2 KiB
TypeScript
44 lines
1.2 KiB
TypeScript
import dayjs from 'dayjs'
|
|
|
|
/**
|
|
* 格式化日期时间
|
|
* @param date 日期
|
|
* @param format 格式,默认为 'YYYY-MM-DD HH:mm:ss'
|
|
* @returns 格式化后的日期字符串
|
|
*/
|
|
export function formatDateTime(date: string | Date | null | undefined, format = 'YYYY-MM-DD HH:mm:ss'): string {
|
|
if (!date) return '-'
|
|
return dayjs(date).format(format)
|
|
}
|
|
|
|
/**
|
|
* 格式化日期
|
|
* @param date 日期
|
|
* @param format 格式,默认为 'YYYY-MM-DD'
|
|
* @returns 格式化后的日期字符串
|
|
*/
|
|
export function formatDate(date: string | Date | null | undefined, format = 'YYYY-MM-DD'): string {
|
|
if (!date) return '-'
|
|
return dayjs(date).format(format)
|
|
}
|
|
|
|
/**
|
|
* 格式化时间
|
|
* @param date 日期
|
|
* @param format 格式,默认为 'HH:mm:ss'
|
|
* @returns 格式化后的时间字符串
|
|
*/
|
|
export function formatTime(date: string | Date | null | undefined, format = 'HH:mm:ss'): string {
|
|
if (!date) return '-'
|
|
return dayjs(date).format(format)
|
|
}
|
|
|
|
/**
|
|
* 获取相对时间
|
|
* @param date 日期
|
|
* @returns 相对时间字符串
|
|
*/
|
|
export function getRelativeTime(date: string | Date | null | undefined): string {
|
|
if (!date) return '-'
|
|
return dayjs(date).fromNow()
|
|
}
|