]>
Commit | Line | Data |
---|---|---|
dd24f1bb C |
1 | import { DatePipe } from '@angular/common' |
2 | ||
f228e9f0 C |
3 | let datePipe: DatePipe |
4 | let intl: Intl.DateTimeFormat | |
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) | |
dd24f1bb C |
27 | } |
28 | ||
29 | function durationToString (duration: number) { | |
30 | const hours = Math.floor(duration / 3600) | |
31 | const minutes = Math.floor((duration % 3600) / 60) | |
32 | const seconds = duration % 60 | |
33 | ||
34 | const minutesPadding = minutes >= 10 ? '' : '0' | |
35 | const secondsPadding = seconds >= 10 ? '' : '0' | |
36 | const displayedHours = hours > 0 ? hours.toString() + ':' : '' | |
37 | ||
38 | return ( | |
39 | displayedHours + minutesPadding + minutes.toString() + ':' + secondsPadding + seconds.toString() | |
40 | ).replace(/^0/, '') | |
41 | } | |
42 | ||
43 | export { | |
f228e9f0 C |
44 | DateFormat, |
45 | ||
dd24f1bb C |
46 | durationToString, |
47 | dateToHuman | |
48 | } |