Last active
April 19, 2024 04:41
-
-
Save westc/4d4bf1d11d0ce074053c7d530d67d7d6 to your computer and use it in GitHub Desktop.
Makes it possible to format a Date object while leveraging the Intl.DateTimeFormat formatter.
This file contains hidden or 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
function formatIntlDate(date, format, options) { | |
return format.replace( | |
/<<|>>|<(\w)(?:\|([^>]+))?>/g, | |
(fullMatch, type, option) => { | |
if (!type) return fullMatch.charAt(0); | |
if (/^[YMDHhmsf]$/.test(type ?? '')) option ??= 'numeric'; | |
const subOptions = {timeZone: options?.timeZone ?? undefined}; | |
let partType; | |
if (type === 'Y') { | |
partType = 'year'; | |
subOptions.year = option; | |
} | |
else if (type === 'M') { | |
partType = 'month'; | |
subOptions.month = option; | |
} | |
else if (type === 'D') { | |
partType = (option === 'numeric' || option === '2-digit') ? 'day' : 'weekday'; | |
subOptions[partType] = option; | |
} | |
else if (type === 'H' || type === 'h') { | |
subOptions.hour = option; | |
partType = 'hour'; | |
subOptions.hourCycle ??= 'h23'; | |
} | |
else if (type === 'm') { | |
subOptions.hour = 'numeric'; // Hack to allow 2-digit minute to work. | |
subOptions.minute = option; | |
partType = 'minute'; | |
} | |
else if (type === 's') { | |
subOptions.minute = 'numeric'; // Hack to allow 2-digit second to work. | |
subOptions.second = option; | |
partType = 'second'; | |
} | |
else if (type === 'f') { | |
partType = 'fractionalSecond'; | |
subOptions.fractionalSecondDigits = option === 'numeric' ? 3 : +option.charAt(0); | |
} | |
else if (type === 'P' || type === 'p') { | |
partType = 'hour'; | |
subOptions.hour ??= 'numeric'; | |
subOptions.hourCycle ??= 'h23'; | |
} | |
else throw new Error(`Unrecognized format: ${fullMatch}`); | |
let output; | |
for (const part of new Intl.DateTimeFormat(options?.locale, subOptions).formatToParts(date)) { | |
if (part.type === partType) { | |
output = part.value; | |
break; | |
} | |
} | |
if (type === 'f' && option === 'numeric') return parseInt(output, 10); | |
if (type === 'h') { | |
output = (parseInt(output, 10) % 12) || 12; | |
return option === '2-digit' ? `0${output}` : output; | |
} | |
if (type === 'P' || type === 'p') { | |
output = parseInt(output, 10) < 12 ? 'am' : 'pm'; | |
if (type === 'P') output = output.toUpperCase(); | |
return option === 'period' ? output.replace(/./g, '$&.') : output; | |
} | |
return output; | |
} | |
); | |
} |
This file contains hidden or 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
const paramSets = [ | |
{ | |
format: 'It is <D|long>, <M|long> <D>, <Y> at <h>:<m|2-digit>:<s|2-digit><P> (<f|numeric>ms).', | |
options: { | |
timeZone: 'America/La_Paz', | |
locale: 'en-US' | |
} | |
}, | |
{ | |
format: 'Hoy es <D|long>, el <D> de <M|long>, <Y> a las <h>:<m|2-digit>:<s|2-digit><P> (<f|numeric>ms).', | |
options: { | |
timeZone: 'America/La_Paz', | |
locale: 'es' | |
} | |
}, | |
{ | |
format: 'Hoy es <D|long>, el <D> de <M|long>, <Y> a las <h>:<m|2-digit>:<s|2-digit><P> (<f|numeric>ms) en Adelaida de Australia.', | |
options: { | |
timeZone: 'Australia/Adelaide', | |
locale: 'es' | |
} | |
} | |
]; | |
for (params of paramSets) { | |
console.log(formatIntlDate(new Date, params.format, params.options), params); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Working on this: