How it Works
Internal resolution logic of dtf() and dt(), and how to extend formats and locales
How it Works
Resolution logic
dtf(format) resolves a canonical format key to an actual Moment format string:
- Reads
date_format_locale(IN / US) andpreferences.is_24_hrs_format(12h / 24h) from the Redux store. - Looks up the key in
DATE_FORMATS(date-only table) → if found, returns the locale-specific format. - If not found, and user prefers 24h, looks in
DATE_TIME_FORMATS_24_HOURS; if 12h, looks inDATE_TIME_FORMAT_12_HOURS. - If still not found, returns
formatunchanged (pass-through).
dt(date, format) simply does moment(date).format(dtf(format)), so all locale and 12h / 24h logic lives in dtf.
How to introduce a new date/time format
-
Choose a canonical key (e.g.
"YYYY-MM-DD"). Call sites always pass this key todt()/dtf(). -
Add the key to the right table(s):
- Date only →
DATE_FORMATSfor bothINandUS. - Date + time (24h) →
DATE_TIME_FORMATS_24_HOURSfor both locales. - Date + time (12h) →
DATE_TIME_FORMAT_12_HOURSfor both locales.
- Date only →
-
Keep 24h and 12h keys in sync — if you add a key to the 24h table, add the same key to the 12h table with the
hh:mm Avariant, sodtf()resolves correctly for both preferences. -
Use only
dt()ordtf()at call sites — no one-offmoment(...).format(...)in feature code.
// Example — adding "YYYY-MM-DD" (date only) to DATE_FORMATS
IN: {
// ...existing entries
"YYYY-MM-DD": "YYYY-MM-DD",
},
US: {
// ...existing entries
"YYYY-MM-DD": "YYYY-MM-DD", // same for ISO-style
},How to introduce a new locale
-
Extend the type
type DateLocale = "IN" | "US" | "UK"; -
Add the locale to all three mapping objects —
DATE_FORMATS,DATE_TIME_FORMATS_24_HOURS, andDATE_TIME_FORMAT_12_HOURS— with the same set of canonical keys as existing locales, mapped to the correct display format for that locale. -
Ensure the session sends the new value — the backend / session must provide the new locale string (e.g.
"UK") indate_format_locale. The helper validates that the locale exists in the tables and throws if not. -
No change to
dt()/dtf()signatures — call sites keep passing the same canonical keys; only the mapping tables grow.
// Example — adding UK locale
type DateLocale = "IN" | "US" | "UK";
const DATE_FORMATS: Record<DateLocale, Record<string, string>> = {
IN: { /* ... */ },
US: { /* ... */ },
UK: {
"DD/MM/YYYY": "DD/MM/YYYY",
"DD-MM-YYYY": "DD-MM-YYYY",
"Do MMM, YYYY": "Do MMM, YYYY",
"dd/MM/yyyy": "dd/MM/yyyy",
DDMMYYYY: "DDMMYYYY",
},
};
// Repeat the same pattern for DATE_TIME_FORMATS_24_HOURS and DATE_TIME_FORMAT_12_HOURS