返回提示词目录第 1513 页

Moment.js 替换迁移方案

帮助开发者盘点日期时间用法,安全迁移到目标库,并覆盖时区、格式、可变性与边界测试。

完整提示词

<context>
Moment.js is in maintenance mode and large, but replacing it is risky because date bugs are silent. The traps: Moment objects are mutable (`m.add(1, 'day')` changes `m`), so code that relied on or was protected from that changes meaning with immutable libraries; format tokens differ (Moment's `YYYY` and `DD` are date-fns `yyyy` and `dd`, and date-fns `YYYY` is the week-numbering year, a classic end-of-December bug); `moment(string)` parses loosely while date-fns and Luxon require a format or ISO input; `moment-timezone` behaviour needs Luxon, the `Intl` API, date-fns-tz or Temporal, and Day.js needs plugins for time zones, UTC, relative time and custom parsing; `diff` rounding, month overflow (January 31 plus one month), start of week per locale, and DST transitions all differ between libraries. Locales in Moment are loaded globally while others import them per call or rely on `Intl`. Temporal is the long-term native answer, but availability depends on the runtimes you support, so a polyfill or a library is still often needed.
</context>

<task>
Replace Moment.js with {{target_library}}.
{{#notes}}
Notes:
{{notes}}
{{/notes}}

1. Inventory every import of `moment` and `moment-timezone` and group the uses: parsing (with and without formats), formatting (list every format string), arithmetic, comparisons and `diff`, start and end of periods, time zone conversion, relative time, locale use, and places where Moment objects cross API boundaries or are stored.
2. Find code that depends on mutability: Moment objects modified after being passed or stored.
3. If no target was given, recommend one from the needs found (bundle size, time zone support, immutability, runtime support for Temporal), with one paragraph of trade-offs, and confirm before continuing.
4. Write a mapping table from each Moment pattern in this codebase to the target, including every format string translated token by token.
5. Before changing code, add or extend tests around the riskiest uses: end-of-month arithmetic, the last days of December and the first days of January formatting, DST transitions in the time zones used, parsing of the real input formats, and locale output.
6. Replace module by module behind a small date utility module if the codebase does not already have one, so the library is used in one place.
7. Remove `moment` from dependencies only when no imports remain, including in tests and scripts.
8. Run the tests in at least two time zones (for example by setting the `TZ` environment variable for the test run) and compare outputs before and after.
</task>

<constraints>
- Do not translate format strings by guessing; check each token against the target library's documentation.
- Do not change what users see (formats, time zone of display) unless asked.
- Keep stored or transmitted date formats exactly the same.
{{> guardrails/no-hardcoding-to-pass-tests}}
{{> guardrails/verify-before-done}}
{{> guardrails/scope-discipline}}
</constraints>

<output_format>
## Inventory
Use counts by group, format strings found, mutability hotspots.
## Choice
Library chosen and why, or the recommendation awaiting confirmation.
## Mapping
Table: Moment pattern, replacement, behaviour difference to watch.
## Changes made
Files changed, utility module, tests added.
## Verification
Test results in each time zone, edge cases covered, bundle size change.
</output_format>

可替换内容

  • {{target_library}}
  • {{#notes}}
  • {{notes}}
  • {{/notes}}
  • {{> guardrails/no-hardcoding-to-pass-tests}}
  • {{> guardrails/verify-before-done}}
  • {{> guardrails/scope-discipline}}

分类与标签

编程开发、JavaScript迁移、日期时间、依赖替换、回归测试

前往 AI 图片创作