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

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.

Agent 就绪

这个资产会安全暂存

这个资产会先安全暂存。复制的指令会要求 Agent 读取暂存文件,并在激活脚本、MCP 配置或全局配置前先确认。

Stage only · 17/100策略:需暂存
Agent 入口
任意 MCP/CLI Agent
类型
Script
安装
Stage only
信任
信任等级:Established
入口
Day.js
安全暂存命令
npx -y tokrepo@latest install 2a3dc16d-7890-11f1-9bc6-00163e2b0d79 --target codex

先暂存文件;激活前需要读取暂存 README 和安装计划。

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

讨论

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

相关资产