-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
the moment dependency accounts for 59% of the bundle size of snyk-to-html pacakge and only used for a simple (and predefined?) formatting option. source: https://bundlephobia.com/package/[email protected]
- Loading branch information
Showing
3 changed files
with
56 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
export function formatDateTime(date, format): string { | ||
if (!date) { | ||
date = new Date(); | ||
} | ||
const day = date.getUTCDate(); | ||
const ordinalSuffix = getOrdinalSuffix(day); | ||
const dayWithSuffix = `${day}${ordinalSuffix}`; | ||
|
||
const hours = date.getUTCHours(); | ||
const minutes = date.getUTCMinutes(); | ||
const seconds = date.getUTCSeconds(); | ||
const ampm = hours >= 12 ? 'pm' : 'am'; | ||
const formattedHours = hours % 12 || 12; | ||
|
||
const monthNames = [ | ||
"January", "February", "March", "April", | ||
"May", "June", "July", "August", | ||
"September", "October", "November", "December" | ||
]; | ||
|
||
const replacements = { | ||
'MMMM': monthNames[date.getUTCMonth()], | ||
'Do': dayWithSuffix, | ||
'YYYY': date.getUTCFullYear(), | ||
'h': formattedHours, | ||
'mm': minutes.toString().padStart(2, '0'), | ||
'ss': seconds.toString().padStart(2, '0'), | ||
'a': ampm, | ||
'z': 'UTC', | ||
'Z': '+00:00' | ||
}; | ||
|
||
return format.replace(/MMMM|Do|YYYY|h|mm|ss|a|z|Z/g, (match) => replacements[match]); | ||
} | ||
|
||
function getOrdinalSuffix(day) { | ||
if (day > 3 && day < 21) { | ||
return 'th'; | ||
} | ||
switch (day % 10) { | ||
case 1: | ||
return 'st'; | ||
case 2: | ||
return 'nd'; | ||
case 3: | ||
return 'rd'; | ||
default: | ||
return 'th'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters