Sân chơi Moment.js và chuyển sang Day.js
- Tệp không rời khỏi thiết bị của bạn
- Chạy trên trình duyệt
- Miễn phí, không cần đăng ký, không hình mờ
Moment.js is in maintenance mode, and its own team recommends a modern library for new projects. Day.js keeps nearly the same API in about 2 KB, so every section here also shows the Day.js equivalent.
Format playground
Format tokens
Click a token to insert it into the format string. Examples use the reference date and locale.
| Token | Example | Meaning | |
|---|---|---|---|
| Year and quarter | |||
| Four-digit year | |||
| Two-digit year | |||
| Quarter (1–4) | |||
| Quarter, ordinal | |||
| Month | |||
| Month (1–12) | |||
| Month, ordinal | |||
| Month, two digits | |||
| Month, short name | |||
| Month, full name | |||
| Day of month and year | |||
| Day of month | |||
| Day of month, ordinal | |||
| Day of month, two digits | |||
| Day of year | |||
| Day of year, three digits | |||
| Day of week | |||
| Day of week (0 = Sunday) | |||
| Weekday, shortest name | |||
| Weekday, short name | |||
| Weekday, full name | |||
| Day of week, by locale | |||
| ISO day of week (1 = Monday) | |||
| Week | |||
| Week of year, by locale | |||
| Week of year, ordinal | |||
| Week of year, two digits | |||
| ISO week of year | |||
| ISO week, two digits | |||
| Week-numbering year, by locale | |||
| ISO week-numbering year | |||
| Hour | |||
| Hour, 0–23 | |||
| Hour, 00–23 | |||
| Hour, 1–12 | |||
| Hour, 01–12 | |||
| Hour, 1–24 | |||
| Hour, 01–24 | |||
| Minute and second | |||
| Minute | |||
| Minute, two digits | |||
| Second | |||
| Second, two digits | |||
| Tenths of a second | |||
| Hundredths of a second | |||
| Milliseconds | |||
| AM / PM | |||
| AM / PM | |||
| am / pm | |||
| Time zone | |||
| UTC offset, ±HH:mm | |||
| UTC offset, ±HHmm | |||
| needs moment-timezone | Time zone abbreviation | ||
| needs moment-timezone | Time zone name | ||
| Unix time | |||
| Unix seconds | |||
| Unix milliseconds | |||
| Localized formats | |||
| Localized time | |||
| Localized time with seconds | |||
| Localized numeric date | |||
| Localized date with month name | |||
| Localized date and time | |||
| Localized date and time with weekday | |||
| Short numeric date | |||
| Short date with month name | |||
| Short date and time | |||
| Short date and time with weekday | |||
| Escaping | |||
| Text in square brackets is printed as is | |||
Parse playground
Leave empty to parse ISO 8601.
Relative time
Each row shifts the reference date, then describes it relative to the reference with from() and calendar().
| Shift | from(ref) | calendar(ref) |
|---|---|---|
| .add(-45, 'second') | ||
| .add(-10, 'minute') | ||
| .add(-3, 'hour') | ||
| .add(-1, 'day') | ||
| .add(-5, 'day') | ||
| .add(-3, 'month') | ||
| .add(-2, 'year') | ||
| .add(30, 'minute') | ||
| .add(2, 'day') | ||
| .add(1, 'month') |
Durations
Recipes
Everyday tasks with live results. “Try” loads one into the code runner below.
| Task | Moment.js |
|---|---|
Format the current time | moment().format('YYYY-MM-DD HH:mm:ss')→ |
Add days | moment('2023-01-01').add(1, 'days').format('YYYY-MM-DD')→ |
Time from now | moment().subtract(7, 'days').fromNow()→ |
First day of this month | moment().startOf('month').format('YYYY-MM-DD')→ |
Last day of this year | moment().endOf('year').format('YYYY-MM-DD')→ |
Start of this week (locale aware) | moment().startOf('week').format('YYYY-MM-DD ddd')→ |
Days between two dates | moment().diff(moment('2022-01-01'), 'days')→ |
Days in this month | moment().daysInMonth()→ |
Quarter of the year | moment().quarter()→ |
Calendar time | moment().add(1, 'days').calendar()→ |
Is a date between two others | moment('2024-12-31').isBetween('2024-01-01', '2025-01-01')→ |
Is it today | moment().isSame(moment(), 'day')→ |
Is it a leap year | moment('2024-01-01').isLeapYear()→ |
Validate a date strictly | moment('2023-02-30', 'YYYY-MM-DD', true).isValid()→ |
Parse a custom format | moment('13/10/2026 14:30', 'DD/MM/YYYY HH:mm').format()→ |
Unix timestamp in seconds | moment().unix()→ |
Current time in UTC | moment.utc().format()→ |
Time in another time zone | needs moment-timezone |
Humanize a duration | moment.duration(90, 'minutes').humanize()→ |
Add a month at the end of a month | moment('2026-01-31').add(1, 'months').format('YYYY-MM-DD')→ |
Call add() without reassigning | (() => { const d = moment('2026-01-01'); d.add(1, 'days'); return d.format('YYYY-MM-DD') })()→ |
Code runner
Type an expression that uses moment. It runs right here in your browser, with every plugin from the recipes enabled and the locale above.
Cách dùng Sân chơi Moment.js và chuyển sang Day.js
Thử định dạng, phân tích, thời gian tương đối và khoảng thời gian của Moment.js, với kết quả Day.js hiện cạnh mọi kết quả Moment. Khác biệt được đánh dấu, để bạn thấy điều gì sẽ đổi trước khi chuyển codebase. Cả hai thư viện chạy trong trình duyệt.
- 1
Đặt “Ngày và giờ tham chiếu” và “Locale”, rồi bật “Coi là UTC” nếu mã của bạn dùng moment.utc().
- 2
Gõ mẫu vào “Chuỗi định dạng”; “Đầu ra” hiện kết quả của Moment và Day.js cho gì, đánh dấu “Cùng đầu ra” hoặc “Đầu ra khác”.
- 3
Xem bảng “Token định dạng” với các token Day.js thiếu hoặc cần plugin, rồi nhấn token để chèn.
- 4
So hai thư viện trong “Sân chơi phân tích”, “Thời gian tương đối” và “Khoảng thời gian”, nơi giá trị Day.js khác được tô.
- 5
Dùng “Công thức” cho mã cạnh nhau của việc thường ngày, và nhấn “Thử” để chạy bản Moment trong “Chạy mã”.
Câu hỏi thường gặp
Moment.js còn được bảo trì không?
Moment.js đang ở chế độ bảo trì, và chính đội ngũ của nó khuyên dùng thư viện hiện đại cho dự án mới. Ghi chú ở trên liên kết tới trang trạng thái dự án Moment và sân chơi Day.js.
Chuyển sang Day.js thường hỏng ở đâu?
Tính thay đổi tại chỗ: add() của Moment đổi đối tượng gốc, còn Day.js trả về đối tượng mới, nên kết quả phải gán lại. Nhiều tính năng cũng cần plugin Day.js, mà mỗi công thức liệt kê.
Những token định dạng nào không có tương đương Day.js?
Qo, Mo, DDD, DDDD, e, E, S và SS. Bảng “Token định dạng” đánh dấu chúng là không có trong Day.js và nêu plugin cần cho các token còn lại.
Vì sao token z và zzz không chạy trong Moment?
Tên múi giờ cần phần bổ sung moment-timezone riêng, mà trang này không nạp, nên bảng hiện “cần moment-timezone”. Day.js cung cấp chúng qua plugin timezone.
Những phiên bản nào được so sánh?
Phiên bản Moment.js và Day.js chính xác được hiện dưới bảng cài đặt.
Mã của tôi có được gửi tới máy chủ không?
Không. Cả hai thư viện và tệp locale nạp vào trang này, và mọi thứ bạn gõ được tính trong trình duyệt.
