Overview

Day.js is an open-source JavaScript library that provides a streamlined API for working with dates and times. It was introduced in 2018 as a lightweight alternative to established libraries like Moment.js, aiming to offer similar functionality with a significantly smaller bundle size. This focus on minimalism makes Day.js particularly well-suited for web development projects where every kilobyte counts towards faster page loads and improved user experience. Its API is designed to be largely compatible with Moment.js, which can simplify migration for developers familiar with that ecosystem.

The library supports common date and time operations, including parsing various input formats, formatting dates into human-readable strings, performing calculations like adding or subtracting time units, and querying date properties. Day.js achieves its small size by focusing on core functionality and offering optional plugins for more specialized use cases, such as advanced formatting or time zone support. This modular approach allows developers to include only the features they need, further optimizing application performance.

Day.js is applicable across various JavaScript environments, from front-end web applications built with frameworks like React, Vue, or Angular, to back-end Node.js services. Its consistent API and predictable behavior make it a reliable choice for managing date-time data throughout a full-stack application. Developers often choose Day.js when they require robust date manipulation capabilities without the overhead associated with larger libraries, prioritizing performance and a lean dependency tree.

The library's design emphasizes immutability, meaning that date manipulation operations return new Day.js objects rather than modifying the original. This functional programming pattern helps prevent unintended side effects and makes code more predictable and easier to debug. For applications requiring precise date calculations, such as scheduling tools, financial applications, or data analytics platforms, Day.js provides the necessary primitives to handle complex temporal logic effectively. Its developer experience is enhanced by clear documentation and a straightforward installation process, enabling quick integration into new and existing projects.

While Day.js offers a compelling balance of features and performance, developers also consider alternatives like date-fns, which provides a collection of pure functions for date manipulation, or Luxon, a modern date library from the Moment.js team that leverages the native Intl object for internationalization. The choice often depends on specific project requirements, such as the need for extensive internationalization support, browser compatibility targets, or existing team familiarity with a particular API paradigm. However, for many common use cases, Day.js provides an efficient and effective solution for date and time management in JavaScript applications.

Key features

  • Lightweight Bundle Size: Day.js has a small core library size, typically under 2KB minified and gzipped, which reduces application load times. This makes it a strong choice for performance-sensitive web applications, as noted by web performance best practices for JavaScript bundles on web.dev.
  • Moment.js-Compatible API: The API design closely mirrors that of Moment.js, easing migration for projects looking to reduce their dependency footprint without a steep learning curve. Developers can often transition with minimal code changes, as described in the Day.js API reference.
  • Date Parsing: Supports parsing dates from various formats, including strings, JavaScript Date objects, and Unix timestamps. It can intelligently interpret common date string formats without explicit format strings.
  • Date Formatting: Provides extensive options for formatting dates into custom strings using tokens (e.g., YYYY-MM-DD HH:mm:ss), allowing for localized and specific output requirements.
  • Date Manipulation: Offers methods to add or subtract years, months, days, hours, minutes, and seconds. This enables calculations like determining a date in the future or past, or finding the difference between two dates.
  • Immutability: All manipulation operations return new Day.js objects, ensuring that original date objects remain unchanged. This functional approach contributes to more predictable code behavior and fewer side effects.
  • Internationalization (i18n): Supports multiple locales through a plugin system, allowing dates to be formatted and displayed according to specific language and regional conventions.
  • Plugin System: Extends core functionality with various plugins for features like time zone support, advanced formatting, or relative time display, allowing developers to include only necessary features and maintain a small bundle.
  • Browser and Node.js Support: Functions reliably in both client-side browser environments and server-side Node.js applications, offering a consistent date utility across the full stack.

Pricing

Day.js is distributed under the MIT License, making it a free and open-source project. There are no licensing fees, usage costs, or commercial tiers associated with its use.

Feature Day.js (As of 2026-05-07)
License MIT License
Cost Free
Commercial Support Community-driven
Core Library Included
Plugins Included

For detailed licensing information, refer to the Day.js homepage.

Common integrations

  • React applications: Day.js is frequently used in React projects for displaying and manipulating dates in UI components, often integrated with state management libraries like Redux or React Context.
  • Vue.js applications: Developers utilize Day.js within Vue components for date formatting, input validation, and dynamic date calculations, sometimes through custom Vue filters or computed properties.
  • Angular applications: Day.js can be integrated into Angular services and components to manage date-time data, providing a consistent API across the application.
  • Node.js APIs: For server-side applications built with Express.js, Fastify, or Hono, Day.js handles date parsing from request bodies, logging timestamps, and managing database date entries.
  • Data Visualization Libraries: When working with charting libraries (e.g., Chart.js, D3.js), Day.js can prepare and format date data for display on axes or tooltips.
  • Form Handling Libraries: Integrated with form libraries like React Hook Form or Formik to standardize date inputs and outputs, ensuring correct data submission and display.

Alternatives

  • Moment.js: A feature-rich, established JavaScript date library that Day.js aims to replace for its smaller size, offering a similar API.
  • date-fns: A modular JavaScript date utility library providing a collection of pure functions for date manipulation, emphasizing immutability and tree-shaking support.
  • Luxon: A modern JavaScript library for dates and times, created by the Moment.js team, which uses native Internationalization API for enhanced locale support and immutability.

Getting started

To begin using Day.js, you first need to install it in your project. This can typically be done via npm or yarn. After installation, you can import Day.js and start performing date and time operations.

Installation

Using npm:

npm install dayjs

Using yarn:

yarn add dayjs

Basic Usage Example

This example demonstrates how to import Day.js, get the current date and time, format it, add a week, and then format the new date. This showcases common operations such as initialization, formatting, and manipulation, which are foundational to using the library effectively.

import dayjs from 'dayjs';

// Get the current date and time
const now = dayjs();
console.log('Current date and time:', now.format('YYYY-MM-DD HH:mm:ss'));

// Parse a specific date string
const specificDate = dayjs('2023-10-26');
console.log('Specific date:', specificDate.format('MMMM D, YYYY'));

// Add 7 days to the current date
const nextWeek = now.add(7, 'day');
console.log('Next week:', nextWeek.format('dddd, MMMM D, YYYY'));

// Subtract 3 months from a specific date
const threeMonthsAgo = specificDate.subtract(3, 'month');
console.log('Three months ago from specific date:', threeMonthsAgo.format('YYYY/MM/DD'));

// Check if a date is before another date
const isBefore = dayjs('2024-01-01').isBefore(dayjs('2023-12-31'));
console.log('Is 2024-01-01 before 2023-12-31?', isBefore);

// Get the start of the month
const startOfMonth = now.startOf('month');
console.log('Start of current month:', startOfMonth.format('YYYY-MM-DD'));

// Use a plugin (e.g., for relative time)
// You would typically import the plugin explicitly:
// import relativeTime from 'dayjs/plugin/relativeTime';
// dayjs.extend(relativeTime);
// console.log('Time from now:', dayjs().subtract(5, 'hour').fromNow());

For more detailed information on installation and API usage, consult the Day.js installation guide and the Day.js API documentation.