From f228e9f064f27301e59533aac48fd6c2e533890d Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Wed, 10 Aug 2022 10:26:20 +0200 Subject: More precise date for jobs --- client/src/app/helpers/utils/date.ts | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) (limited to 'client/src/app/helpers') 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 @@ import { DatePipe } from '@angular/common' -const datePipe = new DatePipe('en') -function dateToHuman (date: string) { - return datePipe.transform(date, 'medium') +let datePipe: DatePipe +let intl: Intl.DateTimeFormat + +type DateFormat = 'medium' | 'precise' + +function dateToHuman (localeId: string, date: Date, format: 'medium' | 'precise' = 'medium') { + if (!datePipe) { + datePipe = new DatePipe(localeId) + } + + if (!intl) { + intl = new Intl.DateTimeFormat(localeId, { + hour: 'numeric', + minute: 'numeric', + second: 'numeric', + year: '2-digit', + month: 'numeric', + day: 'numeric', + fractionalSecondDigits: 3 + }) + } + + if (format === 'medium') return datePipe.transform(date, format) + if (format === 'precise') return intl.format(date) } function durationToString (duration: number) { @@ -20,6 +41,8 @@ function durationToString (duration: number) { } export { + DateFormat, + durationToString, dateToHuman } -- cgit v1.2.3