Dayjs Date Format: A Practical Guide to format() and Its Tokens
Learn how to format dates with Day.js: how format() works, which tokens to use, how to escape literal text, add plugins and locales, and check the output.
Day.js is a small JavaScript library for working with dates. Its format() method converts a date object into a text string that follows a pattern you choose, such as 2026-10-31 or Saturday, October 31, 2026. This guide explains what format() does, which pattern tokens you need, and how to check that the output is correct. It also covers the common mistakes that change the result without any error.
About the examples: they are illustrative and were not executed for this article. They follow the documented behavior of Day.js. Exact output depends on your time zone, the active locale, and the Day.js version and plugins you have installed. The brief for this guide included no version-specific test data, so run the examples in your own project before you rely on them.
What format() does to your date text
format() takes a Day.js object as input. That object can be created from a date string, a timestamp, a native Date, or the current moment. format() also takes a pattern string. It returns a new plain string and leaves the original date object unchanged. Each recognized token in the pattern, such as YYYY or MM, is replaced with the matching part of the date. Characters that are not tokens, such as hyphens, slashes, commas and spaces, are copied into the output exactly as written.
If you call format() with no pattern, Day.js returns an ISO 8601 style string with the local UTC offset, for example 2026-10-31T14:05:09-04:00. The offset in that example is illustrative. Yours will reflect your environment's time zone.
Step-by-step: formatting a date with Day.js
- 1. Install the package in your project, for example with npm install dayjs.
- 2. Import it: import dayjs from 'dayjs';
- 3. Create a date object, for example dayjs() for now, or dayjs('2026-10-31T14:05:09') for a specific moment.
- 4. Call .format() with a pattern string built from the tokens in the table below.
- 5. Check the result against a date whose correct output you already know, and confirm the input was valid with .isValid().
import dayjs from 'dayjs';
// Example input (local time)
const date = dayjs('2026-10-31T14:05:09');
date.format('YYYY-MM-DD'); // '2026-10-31'
date.format('MM/DD/YYYY'); // '10/31/2026'
date.format('dddd, MMMM D, YYYY'); // 'Saturday, October 31, 2026'
date.format('h:mm A'); // '2:05 PM'
date.format('HH:mm:ss'); // '14:05:09'
Core format tokens
The following tokens work in core Day.js without plugins. Tokens are case-sensitive, so MM and mm mean different things. The example values in the table refer to the sample date 2026-10-31 14:05:09.123 in the default English locale.
| Token | Meaning | Example output |
| YY | Two-digit year | 26 |
| YYYY | Four-digit year | 2026 |
| M | Month, 1–12 | 10 |
| MM | Month, two digits | 10 |
| MMM | Short month name | Oct |
| MMMM | Full month name | October |
| D | Day of month, 1–31 | 31 |
| DD | Day of month, two digits | 31 |
| d | Day of week, 0 (Sunday) to 6 | 6 |
| dd | Minimal weekday name | Sa |
| ddd | Short weekday name | Sat |
| dddd | Full weekday name | Saturday |
| H | Hour, 0–23 | 14 |
| HH | Hour, 0–23, two digits | 14 |
| h | Hour, 1–12 | 2 |
| hh | Hour, 1–12, two digits | 02 |
| m | Minute, 0–59 | 5 |
| mm | Minute, two digits | 05 |
| s | Second, 0–59 | 9 |
| ss | Second, two digits | 09 |
| SSS | Milliseconds, three digits | 123 |
| Z | UTC offset with colon | -04:00 (depends on time zone) |
| ZZ | UTC offset without colon | -0400 (depends on time zone) |
| A | AM/PM, uppercase | PM |
| a | am/pm, lowercase | pm |
Common US and international patterns
date.format('MM/DD/YYYY'); // US numeric: '10/31/2026'
date.format('MMM D, YYYY'); // US short text: 'Oct 31, 2026'
date.format('MMMM D, YYYY h:mm A'); // US long: 'October 31, 2026 2:05 PM'
date.format('YYYY-MM-DD'); // ISO date: '2026-10-31'
date.format('DD.MM.YYYY'); // Dotted day-first: '31.10.2026'
date.format('YYYY-MM-DD HH:mm'); // Sortable log style: '2026-10-31 14:05'
For dates that will be sorted as text, such as file names or log lines, use year-first patterns with two-digit padded fields (YYYY-MM-DD, HH, mm). With these patterns, alphabetical order matches chronological order.
Adding literal text with square brackets
Letters inside a pattern can be interpreted as tokens. For example, the a in the word at would be replaced with am or pm. To keep words or characters exactly as typed, wrap them in square brackets. The brackets themselves do not appear in the output.
date.format('[Today is] dddd'); // 'Today is Saturday'
date.format('MMM D [at] h:mm a'); // 'Oct 31 at 2:05 pm'
date.format('MMM D at h:mm a'); // Unintended: the a in at becomes pm, so the result reads 'Oct 31 pmt 2:05 pm'
date.format('YYYY-MM-DD[T]HH:mm'); // '2026-10-31T14:05'
Extra tokens through plugins
Some formatting options are not built into core Day.js and need a plugin. You load a plugin once with dayjs.extend(). If the plugin is not loaded, these tokens are not recognized and may appear in the output as plain text.
- advancedFormat adds tokens such as Q (quarter), Do (day with ordinal suffix, like 31st), k and kk (hour 1–24), X (Unix timestamp in seconds) and x (Unix timestamp in milliseconds).
- localizedFormat adds locale-aware shortcuts: LT, LTS, L, LL, LLL and LLLL.
- utc lets you create and format dates in UTC rather than local time.
- customParseFormat lets you parse an input string by giving its pattern. Use it when the source text is not in ISO format.
import dayjs from 'dayjs';
import advancedFormat from 'dayjs/plugin/advancedFormat';
import localizedFormat from 'dayjs/plugin/localizedFormat';
dayjs.extend(advancedFormat);
dayjs.extend(localizedFormat);
const date = dayjs('2026-10-31T14:05:09');
date.format('Do [of] MMMM'); // '31st of October'
date.format('[Q]Q YYYY'); // 'Q4 2026'
date.format('L'); // '10/31/2026' (English locale)
date.format('LL'); // 'October 31, 2026'
date.format('LT'); // '2:05 PM'
Converting dates from one text format to another
A common extraction task is to read a date from text, such as 31/10/2026, and rewrite it in another format. Day.js reliably parses ISO strings like 2026-10-31 on its own. For other layouts, load customParseFormat and pass the input pattern as the second argument. Without that plugin, a non-ISO string falls back to the JavaScript engine's own parsing. That parsing can be inconsistent and can swap day and month.
import dayjs from 'dayjs';
import customParseFormat from 'dayjs/plugin/customParseFormat';
dayjs.extend(customParseFormat);
const parsed = dayjs('31/10/2026', 'DD/MM/YYYY');
parsed.isValid(); // true
parsed.format('YYYY-MM-DD'); // '2026-10-31'
parsed.format('MM/DD/YYYY'); // '10/31/2026'
Changing the language of month and weekday names
Day.js uses English by default. To output month and weekday names in another language, import that locale file and then activate it. You can activate it globally with dayjs.locale(), or for a single date with .locale(). Numeric tokens such as YYYY and MM are not affected by the locale. Name tokens and localized shortcuts like L are affected.
import dayjs from 'dayjs';
import 'dayjs/locale/es';
dayjs('2026-10-31').locale('es').format('dddd D [de] MMMM');
// Spanish weekday and month names, e.g. 'sábado 31 de octubre'
Verifying the result
- Call .isValid() before formatting input that comes from users or other systems. Formatting an invalid date returns the string Invalid Date and does not throw an error.
- Test with a date where every field is unambiguous, such as day 31 and month 10, so you can spot swapped day and month values.
- Test a date with single-digit values, such as 2026-03-05 09:07, to confirm that padded (MM, DD, HH) or unpadded (M, D, H) output is what you expect.
- Test with an afternoon hour to confirm the choice between the 12-hour (h with A) and 24-hour (HH) clock.
- If the output must be in UTC or a specific zone, compare the result with a known reference value. Local formatting depends on the time zone of the machine or browser.
Mistakes that silently change the output
- Using mm for the month: mm means minutes. Use MM for the month.
- Using lowercase yyyy or dd/mm/yyyy copied from other libraries: Day.js year tokens are uppercase (YYYY), and dd is the minimal weekday name, not the day of the month. Use DD.
- Forgetting brackets around literal words that contain token letters, such as at, day or Q.
- Using plugin tokens like Do, Q or L without loading the plugin.
- Expecting UTC output from plain format(): core Day.js formats in local time unless you use the utc plugin.
date.format('YYYY-mm-DD'); // Wrong: minutes in the middle, e.g. '2026-05-31'
date.format('YYYY-MM-DD'); // Right: '2026-10-31'