How it Works

Internal resolution logic of dtf() and dt(), and how to extend formats and locales

👤 Rahul Bhangale📅 Updated: Mar 13, 2026🏷️ util

How it Works

Resolution logic

dtf(format) resolves a canonical format key to an actual Moment format string:

  1. Reads date_format_locale (IN / US) and preferences.is_24_hrs_format (12h / 24h) from the Redux store.
  2. Looks up the key in DATE_FORMATS (date-only table) → if found, returns the locale-specific format.
  3. If not found, and user prefers 24h, looks in DATE_TIME_FORMATS_24_HOURS; if 12h, looks in DATE_TIME_FORMAT_12_HOURS.
  4. If still not found, returns format unchanged (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

  1. Choose a canonical key (e.g. "YYYY-MM-DD"). Call sites always pass this key to dt() / dtf().

  2. Add the key to the right table(s):

    • Date only → DATE_FORMATS for both IN and US.
    • Date + time (24h) → DATE_TIME_FORMATS_24_HOURS for both locales.
    • Date + time (12h) → DATE_TIME_FORMAT_12_HOURS for both locales.
  3. 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 A variant, so dtf() resolves correctly for both preferences.

  4. Use only dt() or dtf() at call sites — no one-off moment(...).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

  1. Extend the type

    type DateLocale = "IN" | "US" | "UK";
  2. Add the locale to all three mapping objectsDATE_FORMATS, DATE_TIME_FORMATS_24_HOURS, and DATE_TIME_FORMAT_12_HOURS — with the same set of canonical keys as existing locales, mapped to the correct display format for that locale.

  3. Ensure the session sends the new value — the backend / session must provide the new locale string (e.g. "UK") in date_format_locale. The helper validates that the locale exists in the tables and throws if not.

  4. 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

On this page