Sân chơi Day.js và bảng định dạng

Thử chuỗi format và parse của Day.js trực tiếp, kèm bảng token, locale và mã có thể sao chép
  • 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ờ
Loading Day.js…

Format playground

Output
 

Format tokens

Click a token to insert it into the format string. Examples use the reference date and locale.

TokenExampleMeaningDay.js
Year and quarter
Four-digit yearcore
Two-digit yearcore
Quarter (1–4)advancedFormat
not in Day.jsQuarter, ordinalnot in Day.js
Month
Month (1–12)core
not in Day.jsMonth, ordinalnot in Day.js
Month, two digitscore
Month, short namecore
Month, full namecore
Day of month and year
Day of monthcore
Day of month, ordinaladvancedFormat
Day of month, two digitscore
not in Day.jsDay of yearnot in Day.js
not in Day.jsDay of year, three digitsnot in Day.js
Day of week
Day of week (0 = Sunday)core
Weekday, shortest namecore
Weekday, short namecore
Weekday, full namecore
not in Day.jsDay of week, by localenot in Day.js
not in Day.jsISO day of week (1 = Monday)not in Day.js
Week
Week of year, by localeadvancedFormat + weekOfYear
Week of year, ordinaladvancedFormat + weekOfYear
Week of year, two digitsadvancedFormat + weekOfYear
ISO week of yearadvancedFormat + isoWeek
ISO week, two digitsadvancedFormat + isoWeek
Week-numbering year, by localeadvancedFormat + weekYear
ISO week-numbering yearadvancedFormat + isoWeek
Hour
Hour, 0–23core
Hour, 00–23core
Hour, 1–12core
Hour, 01–12core
Hour, 1–24advancedFormat
Hour, 01–24advancedFormat
Minute and second
Minutecore
Minute, two digitscore
Secondcore
Second, two digitscore
not in Day.jsTenths of a secondnot in Day.js
not in Day.jsHundredths of a secondnot in Day.js
Millisecondscore
AM / PM
AM / PMcore
am / pmcore
Time zone
UTC offset, ±HH:mmcore
UTC offset, ±HHmmcore
Time zone abbreviationadvancedFormat + timezone
Time zone nameadvancedFormat + timezone
Unix time
Unix secondsadvancedFormat
Unix millisecondsadvancedFormat
Localized formats
Localized timelocalizedFormat
Localized time with secondslocalizedFormat
Localized numeric datelocalizedFormat
Localized date with month namelocalizedFormat
Localized date and timelocalizedFormat
Localized date and time with weekdaylocalizedFormat
Short numeric datelocalizedFormat
Short date with month namelocalizedFormat
Short date and timelocalizedFormat
Short date and time with weekdaylocalizedFormat
Escaping
Text in square brackets is printed as iscore

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().

Shiftfrom(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.

TaskDay.js
Format the current time
dayjs().format('YYYY-MM-DD HH:mm:ss')
→
Add days
dayjs('2023-01-01').add(1, 'day').format('YYYY-MM-DD')
→
Time from now
Day.js: relativeTime
dayjs().subtract(7, 'day').fromNow()
→
First day of this month
dayjs().startOf('month').format('YYYY-MM-DD')
→
Last day of this year
dayjs().endOf('year').format('YYYY-MM-DD')
→
Start of this week (locale aware)
dayjs().startOf('week').format('YYYY-MM-DD ddd')
→
Days between two dates
dayjs().diff(dayjs('2022-01-01'), 'day')
→
Days in this month
dayjs().daysInMonth()
→
Quarter of the year
Day.js: quarterOfYear
dayjs().quarter()
→
Calendar time
Day.js: calendar
dayjs().add(1, 'day').calendar()
→
Is a date between two others
Day.js: isBetween
dayjs('2024-12-31').isBetween('2024-01-01', '2025-01-01')
→
Is it today
Day.js: isToday
dayjs().isToday()
→
Is it a leap year
Day.js: isLeapYear
dayjs('2024-01-01').isLeapYear()
→
Validate a date strictly
Day.js: customParseFormat
dayjs('2023-02-30', 'YYYY-MM-DD', true).isValid()
→
Parse a custom format
Day.js: customParseFormat
dayjs('13/10/2026 14:30', 'DD/MM/YYYY HH:mm').format()
→
Unix timestamp in seconds
dayjs().unix()
→
Current time in UTC
Day.js: utc
dayjs.utc().format()
→
Time in another time zone
Day.js: utc + timezone + advancedFormat
dayjs().tz('America/New_York').format('YYYY-MM-DD HH:mm z')
→
Humanize a duration
Day.js: duration + relativeTime
dayjs.duration(90, 'minutes').humanize()
→
Add a month at the end of a month
dayjs('2026-01-31').add(1, 'month').format('YYYY-MM-DD')
→
Call add() without reassigning
(() => { const d = dayjs('2026-01-01'); d.add(1, 'day'); return d.format('YYYY-MM-DD') })()
→

Code runner

Type an expression that uses dayjs. It runs right here in your browser, with every plugin from the recipes enabled and the locale above.

Result

Cách dùng Sân chơi Day.js và bảng định dạng

Thử định dạng, phân tích, thời gian tương đối và khoảng thời gian của Day.js với một ngày bạn chọn, đầu ra cập nhật khi bạn gõ. Mỗi kết quả kèm các dòng import và dayjs.extend() nó cần, và bảng token cho biết mỗi token định dạng cần plugin nào.

  1. 1

    Đặt “Ngày và giờ tham chiếu” (hoặc nhấn “Bây giờ”), chọn “Locale”, và bật “Coi là UTC” nếu mã của bạn dùng dayjs.utc().

  2. 2

    Gõ mẫu vào “Chuỗi định dạng” hoặc nhấn một mức sẵn; nhấn token bất kỳ trong “Token định dạng” để chèn tại con trỏ.

  3. 3

    Sao chép mã được tạo dưới “Đầu ra”, vốn đã gồm các import plugin cần thiết.

  4. 4

    Trong “Sân chơi phân tích”, nhập “Chuỗi ngày” và “Định dạng mong đợi”, rồi bật “Phân tích nghiêm” để từ chối ngày không tồn tại.

  5. 5

    Nhấn “Thử” trên “Công thức” bất kỳ để nạp vào “Chạy mã”, rồi sửa và xem kết quả cập nhật.

Câu hỏi thường gặp

Token định dạng cần plugin nào?

Cột Day.js của “Token định dạng” ghi core hoặc tên plugin, ví dụ advancedFormat cho Do, Q và X, hoặc localizedFormat cho L và LLLL. Đoạn mã thêm dòng import và dayjs.extend() tương ứng cho bạn.

Vì sao một token chạy ở đây nhưng ra sai trong dự án của tôi?

Sân chơi này nạp mọi plugin. Lõi Day.js chỉ thay các token nó biết, nên không có advancedFormat thì Q vẫn là Q và Do thành số ngày cộng chữ o lạc. Hãy extend plugin mà bảng nêu; một số token Moment, như DDD hoặc E, không tồn tại trong Day.js.

Vì sao 2023-02-30 được chấp nhận là ngày hợp lệ?

Không có chế độ nghiêm, Day.js đẩy ngày không tồn tại sang tháng sau. Bật “Phân tích nghiêm” (đối số thứ ba, true, với plugin customParseFormat) thì cùng đầu vào được báo là không hợp lệ.

Làm sao đưa chữ thuần vào định dạng?

Bọc trong ngoặc vuông. Ví dụ, [Week] W, GGGG in chữ Week rồi số tuần ISO và năm.

Trình chạy mã có gửi mã của tôi đi đâu không?

Không. Day.js, các plugin và tệp locale chạy trong trang này, và biểu thức của bạn được tính trong trình duyệt với mọi plugin mà các công thức dùng đều được bật.

Tôi có chuyển giờ sang múi khác được không?

Công thức “Giờ ở múi giờ khác” hiện dayjs().tz() với plugin utc và timezone. Để so nhiều múi cạnh nhau, hãy dùng “Chuyển múi giờ”.