Date formatting cleanup, added utility functions to avoid repetition.

This commit is contained in:
Leo Vasanko
2025-08-24 10:03:08 -06:00
parent c77f6af131
commit 4b2a2d0b36
3 changed files with 42 additions and 45 deletions

View File

@@ -287,6 +287,37 @@ function lunarPhaseSymbol(date) {
}
// Exports -----------------------------------------------------------------
/**
* Format date as short localized string (e.g., "Jan 15")
*/
function formatDateShort(date) {
return date.toLocaleDateString(undefined, { month: 'short', day: 'numeric' })
.replace(/, /, ' ')
}
/**
* Format date as long localized string with optional year (e.g., "Mon Jan 15" or "Mon Jan 15, 2025")
*/
function formatDateLong(date, includeYear = false) {
const opts = {
weekday: 'short',
month: 'short',
day: 'numeric',
...(includeYear ? { year: 'numeric' } : {}),
}
return date.toLocaleDateString(undefined, opts)
}
/**
* Format date as today string (e.g., "Monday\nJanuary 15")
*/
function formatTodayString(date) {
const formatted = date
.toLocaleDateString(undefined, { weekday: 'long', month: 'long', day: 'numeric' })
.replace(/,? /, '\n')
return formatted.charAt(0).toUpperCase() + formatted.slice(1)
}
export {
// constants
monthAbbr,
@@ -311,6 +342,9 @@ export {
reorderByFirstDay,
getLocalizedMonthName,
formatDateRange,
formatDateShort,
formatDateLong,
formatTodayString,
lunarPhaseSymbol,
// iso helpers re-export
getISOWeek,