Migrating from Moment.js to Dayjs

A calm, step-by-step plan for replacing Moment.js with Dayjs, including the differences that catch teams out and a testing strategy that keeps releases safe.

Updated 15 November 20266 min readMigration
Laptop with code and a coffee cup on a desk during a Dayjs migration

Moment.js served the JavaScript community superbly for many years, but its maintainers now describe it as a legacy project in maintenance mode and recommend alternatives for new work. Its size and mutable design also sit awkwardly with modern performance budgets and framework patterns. For most teams, Dayjs is the most natural successor, because its API was deliberately modelled on Moment.js. This guide sets out a calm, step-by-step migration plan, highlights the differences that genuinely matter and suggests a testing strategy that keeps every release safe.

Why teams move from Moment.js to Dayjs

Three motivations come up again and again. The first is bundle size: Moment.js weighs around 59 kB minified, or 19 kB gzipped, before locales, and considerably more with them, while the core of Dayjs is about 7 kB minified, or 3 kB gzipped. The second is immutability: Moment objects change in place, which causes subtle bugs when a date is shared between components. The third is longevity: choosing an actively maintained library reduces risk over the life of a product.

Crucially, the similarity of the two APIs keeps the cost of switching low. Many calls work unchanged, so migration is usually measured in days rather than months, even in large codebases.

Step 1: audit your Moment.js usage

Begin by understanding what you actually use. Search the codebase for imports of moment and for calls such as .tz(, .fromNow(, .duration(, .isBetween( and .locale(. Make a short list of the features in play, because each will map to either the Dayjs core or a specific plugin.

Also note any third-party packages that depend on Moment.js, such as older date pickers or charting libraries. Some offer adapters for Dayjs; others may need replacing or can continue to use Moment.js internally while your own code moves on. A clear inventory prevents surprises later in the project.

Step 2: install Dayjs and create a setup module

Install the package as described in our installation guide, then create a single module that configures Dayjs with every plugin your audit identified. This module becomes the only place where plugins are loaded.

// src/lib/date.js
import dayjs from 'dayjs'
import utc from 'dayjs/plugin/utc'
import timezone from 'dayjs/plugin/timezone'
import relativeTime from 'dayjs/plugin/relativeTime'
import customParseFormat from 'dayjs/plugin/customParseFormat'
import localizedFormat from 'dayjs/plugin/localizedFormat'
import duration from 'dayjs/plugin/duration'
import isBetween from 'dayjs/plugin/isBetween'
import advancedFormat from 'dayjs/plugin/advancedFormat'

;[utc, timezone, relativeTime, customParseFormat,
  localizedFormat, duration, isBetween, advancedFormat].forEach(p => dayjs.extend(p))

export default dayjs

Step 3: map Moment.js features to Dayjs plugins

Most of what Moment.js offers is available in Dayjs, either in the core or through an official plugin. The common mappings are:

  • Parsing with a format string, as in moment('15/11/2026', 'DD/MM/YYYY'), requires CustomParseFormat.
  • fromNow(), from(), toNow() and to() require RelativeTime.
  • moment.utc() requires the UTC plugin; moment-timezone features require the Timezone plugin.
  • moment.duration() requires Duration.
  • Tokens such as Do, Q, X require AdvancedFormat; L, LL, LLL require LocalizedFormat.
  • isBetween, isSameOrBefore, isSameOrAfter each have their own plugin.
  • calendar() requires the Calendar plugin; moment.min() and moment.max() require MinMax.
  • ISO weeks require IsoWeek; quarters require QuarterOfYear.

Our plugins guide describes each of these in detail, along with smaller plugins you may find useful along the way.

Step 4: replace imports and handle immutability

With the setup module in place, replace each import moment from 'moment' with an import of your configured instance, and rename calls from moment( to dayjs(. For straightforward code, that is genuinely all that is required.

The one behavioural difference that demands care is immutability. Moment.js code sometimes relies on mutation without making it obvious:

// Moment.js: mutates `date`
const date = moment()
date.add(1, 'day')
console.log(date.format())   // tomorrow

// dayjs: must reassign
let date = dayjs()
date = date.add(1, 'day')
console.log(date.format())   // tomorrow

Search specifically for statements that call add, subtract, startOf, endOf, set, locale or utc without using the returned value. Each is a place where the code silently depended on mutation. Conversely, Moment.js code that called .clone() defensively can simply drop those calls, since every Dayjs operation already returns a fresh instance. Our manipulation and comparison guide explains the immutable model in more depth.

Step 5: review other API differences

A small number of further differences are worth checking:

  • Lenient parsing. Moment.js attempts to parse almost any string, falling back to the native Date parser with a deprecation warning. Dayjs is stricter, so unusual strings that previously “worked” need an explicit format.
  • Global configuration. Settings such as moment.updateLocale map to the UpdateLocale plugin, and relative-time thresholds are configured when extending RelativeTime.
  • Locale imports. Moment.js locale files are replaced with dayjs/locale/ imports, and some codes differ slightly. Our locales guide explains loading and switching.
  • Type checks. moment.isMoment() becomes dayjs.isDayjs().
  • Getters and setters. These follow the same names, but some rarely used units and plural aliases require the PluralGetSet plugin.

Step 6: migrate locales and time zones

If your application uses moment-timezone, the Timezone plugin replaces it in most cases. The main API change is that moment.tz(value, zone) becomes dayjs.tz(value, zone), and conversion with .tz(zone) works in the same way. Dayjs relies on the internationalisation support built into browsers and Node.js rather than bundling its own zone database, which removes a large file from your bundle. Review the time zones and UTC guide for daylight saving considerations.

For locales, import each language your application supports from dayjs/locale/ and set the global or instance locale exactly as you did with Moment.js. Then compare formatted output for a few representative dates in each language to confirm that month names, ordinals and relative phrases read correctly.

Step 7: test thoroughly

A disciplined testing strategy turns migration from a leap of faith into a routine change. Before replacing any code, write characterisation tests that capture the current output of your most important date functions: formatting helpers, deadline calculations, report ranges and anything involving time zones. Use fixed dates rather than the current time, and run the tests in a fixed zone by setting the TZ environment variable.

// package.json
"scripts": {
  "test": "TZ=UTC vitest run",
  "test:karachi": "TZ=Asia/Karachi vitest run"
}

After switching, the same tests should pass unchanged. Include edge cases that commonly differ between libraries: month-end arithmetic, leap years, invalid input, daylight saving transitions and the first week of the year. Where output legitimately changes, for instance because Dayjs is stricter about parsing, update the expectation deliberately and note the reason.

Step 8: roll out gradually and remove Moment.js

In a large codebase there is no need to migrate everything at once. Dayjs and Moment.js can coexist while you move one feature or directory at a time. Convert values at the boundaries with dayjs(momentInstance.toDate()) or moment(dayjsInstance.toDate()) until each area is complete.

Once no imports of Moment.js remain in your own code, remove the package and check your bundle analyser. Many teams see a substantial reduction in their main bundle, which translates directly into faster first loads, particularly on mobile connections. A lint rule that forbids new Moment.js imports prevents the dependency from creeping back.

A quick migration checklist

  1. Inventory Moment.js features and dependent packages.
  2. Install Dayjs and create a single setup module with required plugins.
  3. Write characterisation tests with fixed dates and zones.
  4. Replace imports and rename calls.
  5. Fix code that relied on mutation; remove unnecessary clone() calls.
  6. Add explicit formats for non-ISO parsing.
  7. Migrate locales and time zones, then compare output.
  8. Remove Moment.js and add a lint rule against reintroduction.

Common questions during migration

Will every Moment.js call work unchanged? Most will, but not all. Calls that depend on mutation, lenient parsing or plugins not yet loaded need attention. The audit and test steps above are designed to surface these early.

Can third-party components still use Moment.js? Yes. Libraries that depend on Moment.js internally can continue to do so while your own code uses Dayjs. Over time, look for versions or alternatives with Dayjs adapters so that you can eventually remove the dependency entirely.

How long does a migration take? Small applications are often migrated in a day. Large codebases with extensive time zone logic may take a few weeks of gradual work. In both cases, the effort is dominated by testing rather than by rewriting code.

Conclusion

Moving from Moment.js to Dayjs is one of the more rewarding refactors a JavaScript team can undertake. The familiar API keeps most code unchanged, while the smaller footprint and immutable design deliver tangible benefits in performance and reliability. Audit your usage, configure plugins in one place, pay close attention to code that relied on mutation and protect the change with characterisation tests. Roll out gradually if the codebase is large, remove Moment.js once the work is done and enjoy a lighter, more predictable foundation for every date in your product.