-
Notifications
You must be signed in to change notification settings - Fork 798
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
Also add local version of short date time format #1211
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -42,7 +42,34 @@ function timeFormatWithShortDate(time: string): string { | |||||||||
const dateString = | ||||||||||
date.getMonth() + 1 + "/" + date.getDate() + "/" + date.getFullYear(); | ||||||||||
const timeString = date.toLocaleTimeString("en-US"); | ||||||||||
return `${dateString} at ${timeString} UTC`; | ||||||||||
} | ||||||||||
|
||||||||||
function localTimeFormatWithShortDate(time: string): string { | ||||||||||
// Adjust the fractional seconds to milliseconds (3 digits) | ||||||||||
time = time.replace(/\.(\d{3})\d*/, ".$1"); | ||||||||||
|
||||||||||
// Append 'Z' to indicate UTC time if not already present | ||||||||||
if (!time.endsWith("Z")) { | ||||||||||
time += "Z"; | ||||||||||
} | ||||||||||
|
||||||||||
const date = new Date(time); | ||||||||||
const localTimezone = Intl.DateTimeFormat().resolvedOptions().timeZone; | ||||||||||
|
||||||||||
const dateString = | ||||||||||
date.getMonth() + 1 + "/" + date.getDate() + "/" + date.getFullYear(); | ||||||||||
|
||||||||||
const timeString = date.toLocaleTimeString("en-US", { | ||||||||||
timeZone: localTimezone, | ||||||||||
}); | ||||||||||
|
||||||||||
return `${dateString} at ${timeString}`; | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider appending the local timezone abbreviation to the formatted string to clarify the timezone context.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider appending 'UTC' to the return string for consistency with other time format functions.
Suggested change
|
||||||||||
} | ||||||||||
|
||||||||||
export { basicLocalTimeFormat, basicTimeFormat, timeFormatWithShortDate }; | ||||||||||
export { | ||||||||||
basicLocalTimeFormat, | ||||||||||
basicTimeFormat, | ||||||||||
timeFormatWithShortDate, | ||||||||||
localTimeFormatWithShortDate, | ||||||||||
}; |
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.
localTimeFormatWithShortDate
is a duplicate ofbasicLocalTimeFormat
with a different date format. Consider extendingbasicLocalTimeFormat
to support multiple date formats.basicLocalTimeFormat
(timeFormat.ts)