Scripts2026年5月15日·1 分钟阅读

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

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

Agent 就绪

这个资产可以被 Agent 直接读取和安装

TokRepo 同时提供通用 CLI 命令、安装契约、metadata JSON、按适配器生成的安装计划和原始内容链接,方便 Agent 判断适配度、风险和下一步动作。

Native · 98/100策略:允许
Agent 入口
任意 MCP/CLI Agent
类型
Skill
安装
Single
信任
信任等级:Established
入口
Day.js Overview
通用 CLI 安装命令
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

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产