Every date your application displays started life somewhere else: a form field, an API response, a spreadsheet import, a database column or a URL parameter. Before you can format, compare or manipulate it, that raw value has to become a dependable date object. Parsing is where most date bugs are born, because input is messy and assumptions are easy to make. This guide explains how Dayjs parses different kinds of input, when you need the CustomParseFormat plugin and how to validate values so that faulty data never reaches your users.
Creating a Dayjs instance
The dayjs() function is both the constructor and the parser. With no arguments it captures the current moment. With an argument it attempts to interpret that value and wrap it in a Dayjs object.
import dayjs from 'dayjs'
dayjs() // now
dayjs('2026-11-15') // ISO date string
dayjs(1763200000000) // milliseconds since epoch
dayjs(new Date(2026, 10, 15)) // native Date
dayjs(dayjs('2026-11-15')) // clone of another instance
Each call returns a fresh, immutable instance. Passing an existing Dayjs object creates a copy rather than sharing a reference, which is handy when you want to derive several values from a single starting point. If you are just getting started, our installation guide shows how to add the library first.
Parsing ISO 8601 strings in Dayjs
ISO 8601 is the format Dayjs understands natively and the one you should prefer wherever you control the data. It orders units from largest to smallest, which makes it unambiguous and sortable.
dayjs('2026-11-15') // midnight local time
dayjs('2026-11-15T14:44') // local date and time
dayjs('2026-11-15T14:44:09.250Z') // UTC, converted to local
dayjs('2026-11-15T14:44:09+05:00') // explicit offset
A subtle point deserves attention. A date-only string such as 2026-11-15 is interpreted by Dayjs as midnight in the local time zone. A string ending in Z or an explicit offset represents an exact instant, which is then expressed in local time. Understanding this distinction prevents the classic bug in which a birthday appears one day early for users west of Greenwich.
Parsing Unix timestamps
APIs frequently send timestamps rather than strings. Dayjs accepts milliseconds directly, and provides dayjs.unix() for values expressed in seconds.
dayjs(1763200000000) // milliseconds
dayjs.unix(1763200000) // seconds
Mixing the two units is a frequent source of dates in January 1970 or in the far future. If a timestamp has ten digits it is almost certainly in seconds; thirteen digits suggests milliseconds. A quick check of the documentation for whichever API you are consuming saves hours of confusion.
Parsing custom formats with CustomParseFormat
Real-world data rarely arrives in tidy ISO form. Users type 15/11/2026, spreadsheets export Nov 15, 2026 and legacy systems produce 20261115. Native JavaScript parsing of such strings is inconsistent across browsers, so Dayjs deliberately avoids guessing. Instead, the CustomParseFormat plugin lets you state the pattern explicitly.
import customParseFormat from 'dayjs/plugin/customParseFormat'
dayjs.extend(customParseFormat)
dayjs('15/11/2026', 'DD/MM/YYYY')
dayjs('Nov 15, 2026', 'MMM D, YYYY')
dayjs('20261115', 'YYYYMMDD')
dayjs('2:44 PM', 'h:mm A')
The tokens are the same ones used for formatting, so anything you learnt in our formatting guide applies here in reverse. Text such as month names is matched against the active locale, which means you can parse French or German month names once the corresponding locale is loaded.
Trying several formats
When input might arrive in more than one shape, pass an array of patterns. Dayjs tries each in turn and uses the first that matches.
dayjs('15-11-2026', ['DD/MM/YYYY', 'DD-MM-YYYY', 'YYYY-MM-DD'])
Order matters. Place the most specific or most common pattern first, and avoid combining formats that could match the same string in different ways, such as DD/MM/YYYY and MM/DD/YYYY. When ambiguity is unavoidable, ask the user to choose a format or provide a date picker instead.
Strict parsing in Dayjs
By default, custom parsing is forgiving. It may accept 2026-13-45 and roll the overflow into the following year. For user input, that leniency is rarely what you want. Passing true as the third argument enables strict mode, in which the string must match the pattern exactly and represent a real calendar date.
dayjs('2026-02-30', 'YYYY-MM-DD', true).isValid() // false
dayjs('2026-02-28', 'YYYY-MM-DD', true).isValid() // true
dayjs('2026-2-28', 'YYYY-MM-DD', true).isValid() // false, padding required
Strict mode is the right default for form validation, import pipelines and anything else where a silently corrected value could cause harm. It surfaces problems at the boundary of your system, where they are cheapest to fix.
Validating parsed dates with isValid()
Every instance has an isValid() method. It returns false when parsing failed, for example when given an empty string, an unrecognised format or NaN. Formatting an invalid instance produces the text “Invalid Date”, which should never appear in a finished interface.
function parseUserDate(input) {
const d = dayjs(input, 'DD/MM/YYYY', true)
return d.isValid() ? d : null
}
Notice that calling dayjs(undefined) returns the current moment rather than an invalid date, because it behaves the same as calling dayjs() with no arguments. When a value might be missing, check for it explicitly before parsing; otherwise an empty field could quietly become today's date.
Parsing objects and arrays
Two optional plugins extend the kinds of input Dayjs accepts. ObjectSupport lets you pass an object with named units, and ArraySupport accepts an array in the order year, month, day, hour and so on. Both are convenient when values arrive as separate fields, such as three dropdowns for day, month and year.
import objectSupport from 'dayjs/plugin/objectSupport'
dayjs.extend(objectSupport)
dayjs({ year: 2026, month: 10, day: 15, hour: 14 })
Note that months are zero-indexed, mirroring the native Date object, so 10 means November. This trips up many developers at least once, and a short comment in the code is a kindness to whoever reads it next.
Parsing in UTC and specific time zones
Sometimes a string has no offset, yet you know it represents UTC, for example a server log. The UTC plugin offers dayjs.utc(), which interprets the input as UTC rather than local time. For named regions, the Timezone plugin adds dayjs.tz(value, zone), which treats the input as local time in that region.
dayjs.utc('2026-11-15 09:44')
dayjs.tz('2026-11-15 14:44', 'Asia/Karachi')
These functions are essential for scheduling and reporting systems that operate across regions. The time zones and UTC guide explores them thoroughly, including the pitfalls around daylight saving transitions.
A robust Dayjs parsing workflow
Bringing these ideas together, a dependable parsing workflow looks like this:
- Prefer ISO 8601 for every interface you control, including APIs and databases.
- Declare formats explicitly for anything else, using CustomParseFormat.
- Enable strict mode for user input and imports.
- Check isValid() immediately after parsing and handle failure gracefully.
- Be clear about time zones, using UTC or a named zone whenever the string lacks an offset.
- Test edge cases such as leap days, month ends and daylight saving changes.
Following these steps keeps uncertainty at the edges of your system. Once a value has passed through this gate, the rest of your code can trust it completely, which makes later operations such as manipulation and comparison far simpler to reason about.
Parsing input from forms and date pickers
HTML date inputs return values in a predictable YYYY-MM-DD form regardless of how the browser displays them, which makes them a natural partner for Dayjs. Parse the value directly and validate it before use. Time inputs return HH:mm, so combining both fields into one ISO-style string, such as `${date}T${time}`, produces a reliable local date and time.
Free-text fields are a different matter. People write dates in many ways, and no parser can read every variation correctly. Where free text is unavoidable, accept a small set of documented formats, parse them strictly, and display the interpreted result back to the user for confirmation. A short message such as “We read this as Sunday, 15 November 2026” catches misunderstandings before they cause harm.
Parsing data from APIs and spreadsheets
External data often arrives in bulk, and a single malformed value can disrupt an entire import. Parse each record individually, collect failures rather than stopping at the first one and report them clearly so that the source can be corrected. Logging the raw string alongside the error saves considerable time during investigation.
Spreadsheet exports deserve special caution. Some tools export dates as serial numbers counting days from a base date, others as locale-specific text. Confirm the format with a sample file before writing the parser, and record the decision in your code so that future maintainers understand why a particular pattern was chosen.
Performance is rarely a concern, because parsing with Dayjs is fast even for tens of thousands of rows. Clarity and correctness matter far more than micro-optimisation in this part of an application.
Conclusion
Parsing is the foundation on which every other date operation rests. Dayjs handles ISO strings, timestamps and native dates out of the box, while CustomParseFormat brings explicit, locale-aware patterns for everything else. Strict mode and isValid() turn questionable input into a clear yes or no, and the UTC and Timezone plugins remove ambiguity about where in the world a value belongs. Treat parsing as a deliberate gate rather than an afterthought and the rest of your date handling becomes calmer, more predictable and much easier to test.