import dayjs from 'dayjs' ; import relativeTime from 'dayjs/plugin/relativeTime' ; import 'dayjs/locale/zh-cn' ; import { onLanguageChange } from '../i18n' ; /**
 * Reference: https://day.js.org/
 */ dayjs . extend ( relativeTime ) ; dayjs . locale ( 'zh-cn' ) ; onLanguageChange ( ( lang ) => { if ( lang === 'en-US' ) { dayjs . locale ( 'en' ) ; return ; } dayjs . locale ( 'zh-cn' ) ; } ) ; /**
 * 是否为当天
 */ export function isToday ( date ) { return dayjs ( date ) . isSame ( dayjs ( ) , 'd' ) ; } /**
 * 获取消息显示时间
 */ export function getMessageTimeDiff ( input ) { const date = dayjs ( input ) ; if ( isToday ( date ) ) { return date . fromNow ( ) ; } else { return date . format ( 'YYYY-MM-DD HH:mm:ss' ) ; } } /**
 * 是否应该显示消息时间
 * 间隔时间大于十五分钟则显示
 */ export function shouldShowMessageTime ( date1 , date2 ) { return Math . abs ( date1 . valueOf ( ) - date2 . valueOf ( ) ) > 15 * 60 * 1000 ; } /**
 * 格式化为 小时:分钟
 */ export function formatShortTime ( date ) { return dayjs ( date ) . format ( 'HH:mm' ) ; } example export function datetimeToNow ( input ) { const date = dayjs ( input ) ; return date . toNow ( ) ; } example export function datetimeFromNow ( input ) { const date = dayjs ( input ) ; return date . fromNow ( ) ; }