import moment , { MomentInput } from 'moment' ; import './date-init' ; /**
 * 日期相关帮助函数
 */ param str export function getFromNow ( str ? ) { const date = str ? moment ( str ) : moment ( ) ; return date . fromNow ( ) ; } export const getShortDiff = ( str ) : string => { const date = str ? moment ( str ) : moment ( ) ; if ( helper . isToday ( date ) ) { return date . format ( 'HH:mm' ) ; } else { return date . fromNow ( ) ; } } ; export const getShortDate = ( str ? ) : string => { const date = str ? moment ( str ) : moment ( ) ; if ( helper . isToday ( date ) ) { return date . format ( 'A hh:mm' ) ; } else { return date . format ( 'MM-DD HH:mm' ) ; } } ; export const getMsgDate = ( str ) : string => { const date = str ? moment ( str ) : moment ( ) ; if ( helper . isToday ( date ) ) { return date . format ( 'HH:mm:ss' ) ; } else { return date . format ( 'YYYY-MM-DD HH:mm:ss' ) ; } } ; export const getFullDate = ( str ? ) : string => { const date = str ? moment ( str ) : moment ( ) ; return date . format ( 'YYYY-MM-DD HH:mm:ss' ) ; } ; export const getSimpleDate = ( str ) : string => { return str ? moment ( str ) . format ( 'YYYY-MM-DD' ) : '' ; } ; export const isToday = ( date ) : boolean => { return moment ( ) . isSame ( date , 'day' ) ; } ; export const getDateDiff = ( prevDate , nextDate ) : number => { return moment ( nextDate ) . valueOf ( ) - moment ( prevDate ) . valueOf ( ) ; } ; /**
 * 判定是否为同一天
 */ export function isSameDate ( prevDate , nextDate ) { return ( moment ( prevDate ) . format ( 'YYYY-MM-DD' ) === moment ( nextDate ) . format ( 'YYYY-MM-DD' ) ) ; } /**
 * 判断是否需要强调时间(两个日期间隔超过设定值)
 */ export const shouleEmphasizeTime = ( prevDate , nextDate ) : boolean => { const diffTime = getDateDiff ( prevDate , nextDate ) ; return diffTime / 1000 / 60 >= 5 ; } ; const helper = { getShortDiff , getShortDate , getMsgDate , getFullDate , getSimpleDate , isToday , getDateDiff , } ; export default helper ;