@@ -2,39 +2,32 @@ import type { Dayjs } from 'dayjs';
2
2
import dayjs from 'dayjs' ;
3
3
import duration from 'dayjs/plugin/duration' ;
4
4
import advancedFormat from 'dayjs/plugin/advancedFormat' ;
5
+ import isSameOrAfter from 'dayjs/plugin/isSameOrAfter' ;
6
+ import isSameOrBefore from 'dayjs/plugin/isSameOrBefore' ;
5
7
6
8
dayjs . extend ( duration ) ;
7
9
dayjs . extend ( advancedFormat ) ;
10
+ dayjs . extend ( isSameOrAfter ) ;
11
+ dayjs . extend ( isSameOrBefore ) ;
8
12
9
13
export function renderDate ( date : Dayjs , now : Dayjs ) : string {
10
- const dayjsDate = dayjs ( date ) ;
11
- const yesterday = now . subtract ( 1 , 'day' ) ;
12
- const tomorrow = now . add ( 1 , 'day' ) ;
13
- let dayOfWeek : string ;
14
+ const d = dayjs ( date ) ;
14
15
15
- if (
16
- now . date ( ) === dayjsDate . date ( ) &&
17
- now . year ( ) === dayjsDate . year ( ) &&
18
- now . month ( ) === dayjsDate . month ( )
19
- ) {
20
- dayOfWeek = 'Today' ;
21
- } else if (
22
- yesterday . date ( ) === dayjsDate . date ( ) &&
23
- yesterday . year ( ) === dayjsDate . year ( ) &&
24
- yesterday . month ( ) === dayjsDate . month ( )
25
- ) {
26
- dayOfWeek = 'Yesterday' ;
27
- } else if (
28
- tomorrow . date ( ) === dayjsDate . date ( ) &&
29
- tomorrow . year ( ) === dayjsDate . year ( ) &&
30
- tomorrow . month ( ) === dayjsDate . month ( )
31
- ) {
32
- dayOfWeek = 'Tomorrow' ;
16
+ let label : string ;
17
+
18
+ if ( d . isSame ( now , 'day' ) ) {
19
+ label = 'Today' ;
20
+ } else if ( d . isSame ( now . subtract ( 1 , 'day' ) , 'day' ) ) {
21
+ label = 'Yesterday' ;
22
+ } else if ( d . isSame ( now . add ( 1 , 'day' ) , 'day' ) ) {
23
+ label = 'Tomorrow' ;
24
+ } else if ( d . isSame ( now , 'week' ) ) {
25
+ label = d . format ( 'dddd' ) ;
33
26
} else {
34
- dayOfWeek = dayjsDate . format ( 'dddd, MMM Do' ) ;
27
+ label = d . format ( 'MMM Do' ) ;
35
28
}
36
29
37
- return dayjsDate . format ( `[ ${ dayOfWeek } ] [at] h:mm A` ) ;
30
+ return ` ${ label } at ${ d . format ( ' h:mm A' ) } ` ;
38
31
}
39
32
40
33
export function fromNow ( date : Dayjs , currentTime : Dayjs ) : string {
@@ -71,10 +64,10 @@ export function fromNow(date: Dayjs, currentTime: Dayjs): string {
71
64
} else if ( totalDaysDiff > 0 ) {
72
65
fromNowStr = `${ daysDiff } ${ daysDiff === 1 ? 'day' : 'days' } ` ;
73
66
if ( hoursDiff > 0 ) {
74
- fromNowStr += ` ${ hoursDiff } ${ hoursDiff === 1 ? 'hr' : 'hrs' } ` ;
67
+ fromNowStr += ` ${ hoursDiff } hr ` ;
75
68
}
76
69
} else if ( hoursDiff > 0 ) {
77
- fromNowStr = `${ hoursDiff } ${ hoursDiff === 1 ? 'hr' : 'hrs' } ` ;
70
+ fromNowStr = `${ hoursDiff } hr ` ;
78
71
if ( minutesDiff > 0 ) {
79
72
fromNowStr += ` ${ minutesDiff } m` ;
80
73
}
0 commit comments