Developer Toolkit

Date Formatter Generator

Pick a date pattern like DD/MM/YYYY or relative time and generate a dependency-free JS date formatting function.

Controls

Format pattern

Tokens: YYYY YY MMMM MMM MM M DDDD DDD DD D HH hh mm ss A a — or type “relative”.

Function name

Sample date

Live preview

Formatted output

13/09/2026

ISO: 2026-09-13T15:17:00.000Z

Relative: 39 seconds ago

Generated JavaScript

const MONTHS = ["January","February","March","April","May","June","July","August","September","October","November","December"];
const DAYS = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];
const pad = (n) => String(n).padStart(2, "0");

export function formatDate(input, pattern = "DD/MM/YYYY") {
  const date = input instanceof Date ? input : new Date(input);
  if (Number.isNaN(date.getTime())) return "";

  const h24 = date.getHours();
  const h12 = h24 % 12 === 0 ? 12 : h24 % 12;

  const tokens = {
    YYYY: String(date.getFullYear()),
    YY: String(date.getFullYear()).slice(-2),
    MMMM: MONTHS[date.getMonth()],
    MMM: MONTHS[date.getMonth()].slice(0, 3),
    MM: pad(date.getMonth() + 1),
    M: String(date.getMonth() + 1),
    DDDD: DAYS[date.getDay()],
    DDD: DAYS[date.getDay()].slice(0, 3),
    DD: pad(date.getDate()),
    D: String(date.getDate()),
    HH: pad(h24),
    H: String(h24),
    hh: pad(h12),
    h: String(h12),
    mm: pad(date.getMinutes()),
    ss: pad(date.getSeconds()),
    A: h24 < 12 ? "AM" : "PM",
    a: h24 < 12 ? "am" : "pm"
  };

  return pattern.replace(/YYYY|YY|MMMM|MMM|MM|M|DDDD|DDD|DD|D|HH|H|hh|h|mm|ss|A|a/g, (t) => tokens[t]);
}

// Example usage
console.log(formatDate(new Date()));            // "13/09/2026"
console.log(formatDate("2024-03-09", "MMM D, YYYY")); // "Mar 9, 2024"