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

Day.js — Lightweight 2kB Date-Time Library for Modern JavaScript

A fast, immutable, chainable date library that offers a Moment.js-compatible API in just 2 kilobytes. Supports plugins for advanced formatting, relative time, and timezone handling.

Prêt pour agents

Staging sûr pour cet actif

Cet actif est d'abord staged. Le prompt copié demande à l'agent d'inspecter les fichiers staged avant d'activer scripts, config MCP ou config globale.

Stage only · 17/100Policy : staging
Surface agent
Tout agent MCP/CLI
Type
Script
Installation
Stage only
Confiance
Confiance : Established
Point d'entrée
Day.js
Commande de staging sûr
npx -y tokrepo@latest install 2a3dc16d-7890-11f1-9bc6-00163e2b0d79 --target codex

Stage les fichiers d'abord; l'activation exige la revue du README et du plan staged.

Introduction

Day.js is a minimalist JavaScript date library that parses, validates, manipulates, and displays dates and times. It was created as a lightweight alternative to Moment.js, offering the same familiar API in a package roughly 60 times smaller. The library is immutable by design, preventing accidental mutation bugs common in date handling code.

What Day.js Does

  • Parses, validates, and formats dates with a chainable API
  • Provides locale-aware formatting for 100+ languages
  • Supports relative time display (e.g., "3 hours ago")
  • Handles timezone conversion and UTC offsets via plugins
  • Offers duration, calendar, and advanced formatting through an extensible plugin system

Architecture Overview

Day.js wraps native JavaScript Date objects in a lightweight immutable wrapper. The core library handles parsing, formatting, and basic manipulation, while all advanced features ship as tree-shakeable plugins. Each operation returns a new Day.js instance rather than mutating the original, making it safe for use in React state and Redux stores.

Self-Hosting & Configuration

  • Install via npm, yarn, or pnpm; also available as a CDN script
  • Import only the plugins you need to keep bundle size minimal
  • Load locale files for internationalization support
  • Works in Node.js, browsers, and serverless environments
  • Compatible with ES modules and CommonJS

Key Features

  • Only 2kB minified and gzipped — ideal for performance-critical apps
  • API compatible with Moment.js, easing migration
  • Immutable operations prevent subtle date mutation bugs
  • Plugin architecture keeps the core small while enabling advanced features
  • Full TypeScript support with built-in type definitions

Comparison with Similar Tools

  • Moment.js — feature-complete but 67kB and mutable; now in maintenance mode
  • date-fns — functional approach with tree-shaking, but larger API surface
  • Luxon — built by Moment maintainers with Intl API focus; heavier bundle
  • Temporal (TC39) — upcoming native standard; not yet widely available

FAQ

Q: Can I migrate from Moment.js to Day.js easily? A: Yes. Day.js was designed to be a drop-in replacement. Most Moment.js code works with minimal changes.

Q: Does Day.js handle timezones? A: The core library works with local and UTC time. For full IANA timezone support, use the dayjs/plugin/timezone plugin.

Q: Is Day.js suitable for server-side use? A: Absolutely. It runs in Node.js and has no browser-specific dependencies.

Q: How does the plugin system work? A: Plugins are loaded with dayjs.extend(plugin). Each plugin adds methods or formatting tokens to the Day.js prototype without bloating the core.

Sources

Fil de discussion

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

Actifs similaires