diff options
Diffstat (limited to 'client/src/app/helpers/utils/date.ts')
-rw-r--r-- | client/src/app/helpers/utils/date.ts | 29 |
1 files changed, 26 insertions, 3 deletions
diff --git a/client/src/app/helpers/utils/date.ts b/client/src/app/helpers/utils/date.ts index 012b959ea..75363cc81 100644 --- a/client/src/app/helpers/utils/date.ts +++ b/client/src/app/helpers/utils/date.ts | |||
@@ -1,8 +1,29 @@ | |||
1 | import { DatePipe } from '@angular/common' | 1 | import { DatePipe } from '@angular/common' |
2 | 2 | ||
3 | const datePipe = new DatePipe('en') | 3 | let datePipe: DatePipe |
4 | function dateToHuman (date: string) { | 4 | let intl: Intl.DateTimeFormat |
5 | return datePipe.transform(date, 'medium') | 5 | |
6 | type DateFormat = 'medium' | 'precise' | ||
7 | |||
8 | function dateToHuman (localeId: string, date: Date, format: 'medium' | 'precise' = 'medium') { | ||
9 | if (!datePipe) { | ||
10 | datePipe = new DatePipe(localeId) | ||
11 | } | ||
12 | |||
13 | if (!intl) { | ||
14 | intl = new Intl.DateTimeFormat(localeId, { | ||
15 | hour: 'numeric', | ||
16 | minute: 'numeric', | ||
17 | second: 'numeric', | ||
18 | year: '2-digit', | ||
19 | month: 'numeric', | ||
20 | day: 'numeric', | ||
21 | fractionalSecondDigits: 3 | ||
22 | }) | ||
23 | } | ||
24 | |||
25 | if (format === 'medium') return datePipe.transform(date, format) | ||
26 | if (format === 'precise') return intl.format(date) | ||
6 | } | 27 | } |
7 | 28 | ||
8 | function durationToString (duration: number) { | 29 | function durationToString (duration: number) { |
@@ -20,6 +41,8 @@ function durationToString (duration: number) { | |||
20 | } | 41 | } |
21 | 42 | ||
22 | export { | 43 | export { |
44 | DateFormat, | ||
45 | |||
23 | durationToString, | 46 | durationToString, |
24 | dateToHuman | 47 | dateToHuman |
25 | } | 48 | } |