1
- import dayjs from "dayjs" ;
1
+ import dayjs , { Dayjs } from "dayjs" ;
2
2
import advancedFormat from "dayjs/plugin/advancedFormat" ;
3
3
import duration from "dayjs/plugin/duration" ;
4
4
import localizedFormat from "dayjs/plugin/localizedFormat" ;
@@ -62,66 +62,56 @@ const UnderlinedTrigger = styled(Popover.Trigger)<StyledLinkProps>`
62
62
${ linkStyles }
63
63
` ;
64
64
65
- const dateStyle = "medium" ;
66
- const timeStyle = "medium" ;
65
+ const formatDateDetails = ( date : Dayjs , timezone ?: string ) : string => {
66
+ const isCurrentYear = dayjs ( ) . year ( ) === date . year ( ) ;
67
+ const formatForCurrentYear = "MMM D, h:mm a" ;
68
+ const formatForPastYear = "MMM D, YYYY, h:mm a" ;
69
+
70
+ if ( isCurrentYear ) {
71
+ if ( timezone ) {
72
+ const dateWithTimezone = date . tz ( timezone ) ;
73
+ return dateWithTimezone
74
+ . format ( formatForCurrentYear )
75
+ . replace ( "am" , "a.m." )
76
+ . replace ( "pm" , "p.m." ) ;
77
+ }
67
78
68
- const createBasicDateDetailsFormatter = ( ) => {
69
- return new Intl . DateTimeFormat ( undefined , {
70
- dateStyle,
71
- timeStyle,
72
- } ) ;
73
- } ;
79
+ return date . format ( formatForCurrentYear ) . replace ( "am" , "a.m." ) . replace ( "pm" , "p.m." ) ;
80
+ }
74
81
75
- const formatDateDetails = ( date : Date , locale ?: Intl . Locale , timeZone ?: string ) => {
76
- let dateDetailsFormatter ;
77
- try {
78
- dateDetailsFormatter = new Intl . DateTimeFormat ( locale , {
79
- dateStyle,
80
- timeStyle,
81
- timeZone,
82
- } ) ;
83
- } catch ( error ) {
84
- if ( ( error as Error ) . message . includes ( "invalid time zone" ) ) {
85
- try {
86
- dateDetailsFormatter = new Intl . DateTimeFormat ( locale , {
87
- dateStyle,
88
- timeStyle,
89
- } ) ;
90
- } catch {
91
- dateDetailsFormatter = createBasicDateDetailsFormatter ( ) ;
92
- }
93
- } else if ( ( error as Error ) . message . includes ( "invalid language tag" ) ) {
94
- try {
95
- dateDetailsFormatter = new Intl . DateTimeFormat ( undefined , {
96
- dateStyle,
97
- timeStyle,
98
- timeZone,
99
- } ) ;
100
- } catch {
101
- dateDetailsFormatter = createBasicDateDetailsFormatter ( ) ;
102
- }
103
- } else {
104
- dateDetailsFormatter = createBasicDateDetailsFormatter ( ) ;
105
- }
82
+ if ( timezone ) {
83
+ const dateWithTimezone = date . tz ( timezone ) ;
84
+ return dateWithTimezone
85
+ . format ( formatForPastYear )
86
+ . replace ( "am" , "a.m." )
87
+ . replace ( "pm" , "p.m." ) ;
106
88
}
89
+ return date . format ( formatForPastYear ) . replace ( "am" , "a.m." ) . replace ( "pm" , "p.m." ) ;
90
+ } ;
107
91
108
- return dateDetailsFormatter . format ( date ) ;
92
+ const formatTimezone = ( date : Dayjs , timezone : string ) : string => {
93
+ return (
94
+ new Intl . DateTimeFormat ( "en-US" , {
95
+ timeZone : timezone ,
96
+ timeZoneName : "short" ,
97
+ } )
98
+ . formatToParts ( date . toDate ( ) )
99
+ . find ( part => part . type === "timeZoneName" ) ?. value ?? date . format ( "z" )
100
+ ) ;
109
101
} ;
110
102
111
103
export type ArrowPosition = "top" | "right" | "left" | "bottom" ;
112
104
113
105
export interface DateDetailsProps {
114
106
date : Date ;
115
- locale ?: Intl . Locale ;
116
107
side ?: ArrowPosition ;
117
108
systemTimeZone ?: string ;
118
109
}
119
110
120
111
export const DateDetails = ( {
121
112
date,
122
- locale,
123
113
side = "top" ,
124
- systemTimeZone,
114
+ systemTimeZone = "America/New_York" ,
125
115
} : DateDetailsProps ) => {
126
116
const dayjsDate = dayjs ( date ) ;
127
117
@@ -155,7 +145,8 @@ export const DateDetails = ({
155
145
< Text size = "md" > Local</ Text >
156
146
< Container justifyContent = "end" >
157
147
< Text size = "md" >
158
- { formatDateDetails ( dayjsDate . toDate ( ) , locale ) } ({ dayjsDate . format ( "z" ) } )
148
+ { formatDateDetails ( dayjsDate ) } (
149
+ { formatTimezone ( dayjsDate , dayjs . tz . guess ( ) ) } )
159
150
</ Text >
160
151
</ Container >
161
152
@@ -165,18 +156,16 @@ export const DateDetails = ({
165
156
166
157
< Container justifyContent = "end" >
167
158
< Text size = "md" >
168
- { formatDateDetails ( systemTime . toDate ( ) , locale , systemTimeZone ) } (
169
- { systemTime . format ( "z" ) } )
159
+ { formatDateDetails ( systemTime , systemTimeZone ) } (
160
+ { formatTimezone ( systemTime , systemTimeZone ) } )
170
161
</ Text >
171
162
</ Container >
172
163
</ >
173
164
) }
174
165
175
166
< Text size = "md" > UTC</ Text >
176
167
< Container justifyContent = "end" >
177
- < Text size = "md" >
178
- { formatDateDetails ( dayjsDate . utc ( ) . toDate ( ) , locale , "UTC" ) }
179
- </ Text >
168
+ < Text size = "md" > { formatDateDetails ( dayjsDate . utc ( ) , "UTC" ) } </ Text >
180
169
</ Container >
181
170
182
171
< Text size = "md" > Unix</ Text >
0 commit comments