-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Web: intl add a date time short formatter #39915
Conversation
The PR changelog entry failed validation: Changelog entry not found in the PR body. Please add a "no-changelog" label to the PR, or changelog lines starting with |
); | ||
} catch (err) { | ||
logger.error('dateTimeShortFormat()', err); | ||
return 'undefined'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- What errors do we expect to be thrown here? I see that other functions in this file do use
try … catch
, but they also useformat
fromdate-fns
and notIntl.DateTimeFormat
. - In case of an error, wouldn't it be better to use some kind of method on
Date
to display something approximate instead of saying"undefined"
? IfIntl.DateTimeFormat().format
cannot fail, then the error handling is not even necessary.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- From what I see, the function throws only when
locales
oroptions
contain invalid values https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/DateTimeFormat#exceptions.
If so, should we catch the error at all? It can come only from our invalid configuration, not from the user input.
@@ -83,3 +83,19 @@ export function displayDateTime(date: Date) { | |||
export function dateToUtc(date: Date) { | |||
return new Date(date.getTime() + date.getTimezoneOffset() * 60 * 1000); | |||
} | |||
|
|||
export function dateTimeShortFormat(date: Date) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It'd be nice to put an example in a JSDoc of how the formatted date is going to look like, say for en-US at least. And maybe mention that it's going to use browser and system settings to determine the actual format that's going to be used.
if (isTest) { | ||
return new Intl.DateTimeFormat('en-US', { timeStyle: 'short' }).format( | ||
date | ||
); | ||
} | ||
return new Intl.DateTimeFormat(undefined, { timeStyle: 'short' }).format( | ||
date | ||
); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if (isTest) { | |
return new Intl.DateTimeFormat('en-US', { timeStyle: 'short' }).format( | |
date | |
); | |
} | |
return new Intl.DateTimeFormat(undefined, { timeStyle: 'short' }).format( | |
date | |
); | |
const DEFAULT_LOCALE = 'en-US'; //(as const on top of the file) | |
... | |
const locale = isTest ? DEFAULT_LOCALE : undefined; | |
return new Intl.DateTimeFormat(locale, { timeStyle: 'short' }).format(date); |
); | ||
} catch (err) { | ||
logger.error('dateTimeShortFormat()', err); | ||
return 'undefined'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- From what I see, the function throws only when
locales
oroptions
contain invalid values https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/DateTimeFormat#exceptions.
If so, should we catch the error at all? It can come only from our invalid configuration, not from the user input.
Adds a date time short formatter helper, that when in
test
mode, useen-US
as a default locale