Day.js
Day.js is a minimalist JavaScript library providing a Moment.js-compatible API for parsing, validating, manipulating, and displaying dates and times. At only 2KB, it offers immutability, chainable methods, and extensive internationalization and plugin support. The current stable version is 1.11.20, and the library is actively maintained with frequent bug fix releases.
Common errors
-
ReferenceError: dayjs is not defined
cause Attempting to use `dayjs` without importing it first.fixAdd `import dayjs from 'dayjs';` at the top of your file. -
TypeError: dayjs(...).isBetween is not a function
cause Attempting to use a plugin-provided method (like `isBetween`, `utc`, `duration`) without importing and extending its respective plugin.fixImport the specific plugin and extend Day.js with it: `import isBetween from 'dayjs/plugin/isBetween'; dayjs.extend(isBetween);` -
Invalid Date
cause Day.js could not parse the provided date string into a valid date object, especially for non-standard formats.fixEnsure the date string is in a standard format (ISO 8601, JavaScript Date string), or use the `CustomParseFormat` plugin for specific formats: `dayjs.extend(customParseFormat); dayjs('25/12/1995', 'DD/MM/YYYY');` -
dayjs is not a function
cause Incorrectly importing Day.js (e.g., `import { dayjs } from 'dayjs'` instead of `import dayjs from 'dayjs'`).fixEnsure you are using the default import for the main Day.js function: `import dayjs from 'dayjs';` -
Locale not applying to dates
cause The desired locale was imported but not activated globally or for the specific Day.js instance.fixAfter importing the locale file (e.g., `import 'dayjs/locale/fr';`), activate it using `dayjs.locale('fr');` for global effect or `.locale('fr')` on an instance for local effect.
Warnings
- gotcha Many common date manipulation and query functions (e.g., `isBetween`, `utc`, `duration`, `advancedFormat`) are not included in the core library. They must be explicitly imported and extended as plugins.
- gotcha Day.js only includes the `en` locale by default. To use other locales, you must import them separately and then activate them globally or per instance.
- gotcha Day.js instances are immutable. Methods like `add()`, `subtract()`, `set()`, `startOf()`, `endOf()` return a *new* Day.js instance rather than modifying the original. Assign the result to a new variable or chain methods.
- gotcha The `diff()` method returns an integer difference by default. To get a floating-point number for the difference, you must pass `true` as the third argument.
- gotcha Day.js parsing is not as strict as some other libraries. For explicit and strict parsing of specific date formats, the `CustomParseFormat` plugin is often required.
Install
-
npm install dayjs -
yarn add dayjs -
pnpm add dayjs
Imports
- dayjs
const dayjs = require('dayjs')import dayjs from 'dayjs'
Quickstart
import dayjs from 'dayjs';
const formattedDate = dayjs().startOf('month').add(1, 'day').set('year', 2018).format('YYYY-MM-DD HH:mm:ss');
console.log(formattedDate);
// Example output: '2018-01-02 00:00:00'