You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
tailchat/shared/utils/date-helper.ts

27 lines
563 B
TypeScript

import dayjs from 'dayjs';
import relativeTime from 'dayjs/plugin/relativeTime'; // 导入插件
import 'dayjs/locale/zh-cn'; // 导入本地化语言
dayjs.extend(relativeTime);
dayjs.locale('zh-cn');
/**
* 是否为当天
*/
export function isToday(date: Date): boolean {
return dayjs(date).isSame(dayjs(), 'd');
}
/**
* 获取消息显示时间
*/
export function getMessageTimeDiff(input: Date): string {
const date = dayjs(input);
if (isToday(input)) {
return date.fromNow();
} else {
return date.format('YYYY-MM-DD HH:mm:ss');
}
}