Cette page est affichée en anglais. Une traduction française est en cours.
ScriptsMay 15, 2026·3 min de lecture

Day.js — Fast 2kB Immutable Date Library for JavaScript

Lightweight Moment.js alternative with the same modern API and full internationalization support.

Prêt pour agents

Cet actif peut être lu et installé directement par les agents

TokRepo expose une commande CLI universelle, un contrat d'installation, le metadata JSON, un plan selon l'adaptateur et le contenu raw pour aider les agents à juger l'adaptation, le risque et les prochaines actions.

Native · 98/100Policy : autoriser
Surface agent
Tout agent MCP/CLI
Type
Skill
Installation
Single
Confiance
Confiance : Established
Point d'entrée
Day.js Overview
Commande CLI universelle
npx tokrepo install 2d5fa1ae-4ff5-11f1-9bc6-00163e2b0d79

Introduction

Day.js is a minimalist JavaScript date library that parses, validates, manipulates, and displays dates and times. It was designed as a drop-in replacement for Moment.js with a nearly identical API but at roughly 2kB minified and gzipped. Day.js uses immutable data structures, avoiding the mutation pitfalls that plagued Moment.js.

What Day.js Does

  • Parses dates from strings, timestamps, Date objects, and ISO 8601 formats
  • Formats and displays dates using familiar token patterns like YYYY-MM-DD
  • Performs date arithmetic such as add, subtract, startOf, and endOf
  • Compares dates with isBefore, isAfter, and isSame helpers
  • Extends functionality through a rich plugin ecosystem covering time zones, relative time, and locales

Architecture Overview

Day.js wraps a native JavaScript Date object in a lightweight immutable wrapper. All mutation methods return new Day.js instances rather than modifying the original. The core library is intentionally small and delegates advanced features to optional plugins loaded via dayjs.extend(). Locale data is also loaded on demand, keeping the default bundle minimal.

Self-Hosting & Configuration

  • Install via npm, yarn, or pnpm; also available as a CDN script
  • Import plugins individually: import utc from 'dayjs/plugin/utc'
  • Load locales on demand: import 'dayjs/locale/de' then dayjs.locale('de')
  • Works in Node.js, browsers, and bundlers with full tree-shaking support
  • TypeScript declarations are included out of the box

Key Features

  • 2kB gzipped core with immutable API design
  • Near-complete Moment.js API compatibility for easy migration
  • 20+ official plugins including UTC, timezone, relative time, and duration
  • Supports 100+ locales with on-demand loading
  • Chainable API for readable date manipulation pipelines

Comparison with Similar Tools

  • Moment.js — larger bundle (70kB), mutable API, now in maintenance mode
  • Luxon — more opinionated with Intl-based formatting, larger footprint (~20kB)
  • date-fns — functional tree-shakeable approach but requires importing individual functions
  • Temporal (TC39) — upcoming native JavaScript API, not yet widely available

FAQ

Q: Can I migrate from Moment.js to Day.js without rewriting my code? A: In most cases yes. The core API is nearly identical. The main differences involve plugins for advanced features like time zones and durations.

Q: Does Day.js handle time zones? A: Yes, through the official utc and timezone plugins which use the IANA time zone database.

Q: Is Day.js suitable for server-side Node.js projects? A: Absolutely. It works in any JavaScript environment including Node.js, Deno, and Bun.

Q: How does Day.js achieve its small size? A: By keeping the core minimal and moving features like locales, relative time, and duration into separate plugins.

Sources

Fil de discussion

Connectez-vous pour rejoindre la discussion.
Aucun commentaire pour l'instant. Soyez le premier à partager votre avis.

Actifs similaires