Skip to content

Guide · Open source

How to use dayjs: install, parse, format and change dates

Updated · written with AI from the project's own documentation and checked against our catalogue data

Day.js is a small JavaScript library that parses, checks, changes and formats dates and times. Its API is largely compatible with Moment.js, so the README notes that anyone who knows Moment.js already knows most of Day.js. This guide shows how to install it and use its core parsing, formatting, arithmetic and locale features.

Day.jsiamkun/dayjs · ★ 48.7k · MIT

Quick start

  1. 1Install the package. Run npm install dayjs --save in your project folder.
  2. 2Import dayjs. Import the library into the file where you need dates.
  3. 3Parse a date. Call dayjs with a date string or with no arguments for the current time.
  4. 4Format the output. Use the format method with a pattern such as YYYY-MM-DD.
  5. 5Manipulate dates. Use add, set and other methods, remembering each returns a new value.
  6. 6Add a locale if needed. Import a locale file such as dayjs/locale/es and call dayjs.locale.
  7. 7Check the docs for details. Use the API reference and plugin list on day.js.org for options not shown here.

Install dayjs with npm

Day.js is installed as a package from npm. The README gives the install command below. It is meant for JavaScript projects, whether run under Node.js or in a browser environment with a bundler.

npm install dayjs --save

Create and parse a date

Call dayjs() with no arguments to get the current date and time, or pass a date string to parse it. The README's parse example uses a plain ISO-style date string.

dayjs('2018-08-08') // parse

Format dates for display

The format method turns a date into a string using a pattern. The README shows that square brackets can wrap literal text inside the pattern.

dayjs().format('{YYYY} MM-DDTHH:mm:ss SSS [Z] A') // display

Change and compare dates

Day.js objects are immutable, so methods such as add return a new value rather than changing the original. You can chain calls, and the README shows get and set with month, plus a query with isBefore.

  • add(1, 'year') manipulates a date by a unit amount.
  • set('month', 3) sets a part of the date, and month() reads it back.
  • isBefore compares two dates.
dayjs().set('month', 3).month() // get & set dayjs().add(1, 'year') // manipulate dayjs().isBefore(dayjs()) // query

Use locales for localized output

Locales are not bundled by default. Import the locale you need, then set it globally with dayjs.locale or for a single instance with .locale().

import 'dayjs/locale/es' // load on demand dayjs.locale('es') // use Spanish locale globally dayjs('2018-05-05').locale('zh-cn').format() // use Chinese Simplified locale in a specific instance

Extend Day.js with plugins

Plugins add features beyond the core. Import a plugin, then register it with dayjs.extend. The README example adds more format tokens through the advancedFormat plugin. The full list is on the documentation site.

import advancedFormat from 'dayjs/plugin/advancedFormat' // load on demand dayjs.extend(advancedFormat) // use plugin dayjs().format('Q Do k kk X x') // more available formats

What people use it for

  • Formatting dates for display in a front-end web page.
  • Adding or subtracting days, months or years from a date.
  • Migrating a project that used Moment.js to a smaller library with a similar API.
  • Showing dates in another language by loading a locale file.
  • Checking whether one date is before another.

Frequently asked questions

Is dayjs free to use?

Day.js is licensed under the MIT License, according to the README's license section. The license file is linked from the repository. Check the LICENSE file for the full terms.

How do I install dayjs?

Run npm install dayjs --save in your project. The README says the library is for JavaScript environments, so it is installed as a package rather than as a standalone app.

Is dayjs a replacement for Moment.js?

The README describes Day.js as an alternative to Moment.js with a largely compatible API. If you know Moment.js, the README says you already know how to use Day.js, though you should check specific methods in the docs.

Does dayjs support timezones?

The README does not detail timezone support beyond what is described there, so check the documentation at day.js.org before relying on timezone features.

Why are my locale changes not working?

Locales are not included in the build unless you import them. Import the locale file, such as dayjs/locale/es, before calling dayjs.locale. The README says this is intentional to keep the build small.

How does dayjs compare to date-fns or Luxon?

The source only lists Moment.js, date-fns and Luxon as comparable projects, without a detailed comparison. Day.js's main stated strengths are its small size and Moment.js-style API.

New agent skills, repos and AI tools — every week

The best new skills and open-source projects, the fastest-growing repos, and one free prompt. Five minutes to read. Unsubscribe anytime.

Our verdict

Day.js suits JavaScript developers who used Moment.js and want a much smaller library, or front-end projects where bundle size matters. If you need detailed timezone handling, check the docs first, and if you need a non-JavaScript or no-code tool, pick something else.

Install options & downloads