Date formatting cleanup, added utility functions to avoid repetition.
This commit is contained in:
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user