สนามทดลอง Day.js และตารางรูปแบบ
- ไฟล์ไม่ออกจากอุปกรณ์ของคุณ
- ทำงานในเบราว์เซอร์
- ฟรี ไม่ต้องสมัคร ไม่มีลายน้ำ
Format playground
Format tokens
Click a token to insert it into the format string. Examples use the reference date and locale.
| Token | Example | Meaning | Day.js |
|---|---|---|---|
| Year and quarter | |||
| Four-digit year | core | ||
| Two-digit year | core | ||
| Quarter (1–4) | advancedFormat | ||
| not in Day.js | Quarter, ordinal | not in Day.js | |
| Month | |||
| Month (1–12) | core | ||
| not in Day.js | Month, ordinal | not in Day.js | |
| Month, two digits | core | ||
| Month, short name | core | ||
| Month, full name | core | ||
| Day of month and year | |||
| Day of month | core | ||
| Day of month, ordinal | advancedFormat | ||
| Day of month, two digits | core | ||
| not in Day.js | Day of year | not in Day.js | |
| not in Day.js | Day of year, three digits | not in Day.js | |
| Day of week | |||
| Day of week (0 = Sunday) | core | ||
| Weekday, shortest name | core | ||
| Weekday, short name | core | ||
| Weekday, full name | core | ||
| not in Day.js | Day of week, by locale | not in Day.js | |
| not in Day.js | ISO day of week (1 = Monday) | not in Day.js | |
| Week | |||
| Week of year, by locale | advancedFormat + weekOfYear | ||
| Week of year, ordinal | advancedFormat + weekOfYear | ||
| Week of year, two digits | advancedFormat + weekOfYear | ||
| ISO week of year | advancedFormat + isoWeek | ||
| ISO week, two digits | advancedFormat + isoWeek | ||
| Week-numbering year, by locale | advancedFormat + weekYear | ||
| ISO week-numbering year | advancedFormat + isoWeek | ||
| Hour | |||
| Hour, 0–23 | core | ||
| Hour, 00–23 | core | ||
| Hour, 1–12 | core | ||
| Hour, 01–12 | core | ||
| Hour, 1–24 | advancedFormat | ||
| Hour, 01–24 | advancedFormat | ||
| Minute and second | |||
| Minute | core | ||
| Minute, two digits | core | ||
| Second | core | ||
| Second, two digits | core | ||
| not in Day.js | Tenths of a second | not in Day.js | |
| not in Day.js | Hundredths of a second | not in Day.js | |
| Milliseconds | core | ||
| AM / PM | |||
| AM / PM | core | ||
| am / pm | core | ||
| Time zone | |||
| UTC offset, ±HH:mm | core | ||
| UTC offset, ±HHmm | core | ||
| Time zone abbreviation | advancedFormat + timezone | ||
| Time zone name | advancedFormat + timezone | ||
| Unix time | |||
| Unix seconds | advancedFormat | ||
| Unix milliseconds | advancedFormat | ||
| Localized formats | |||
| Localized time | localizedFormat | ||
| Localized time with seconds | localizedFormat | ||
| Localized numeric date | localizedFormat | ||
| Localized date with month name | localizedFormat | ||
| Localized date and time | localizedFormat | ||
| Localized date and time with weekday | localizedFormat | ||
| Short numeric date | localizedFormat | ||
| Short date with month name | localizedFormat | ||
| Short date and time | localizedFormat | ||
| Short date and time with weekday | localizedFormat | ||
| Escaping | |||
| Text in square brackets is printed as is | core | ||
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 | Day.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.
วิธีใช้ สนามทดลอง Day.js และตารางรูปแบบ
ลองการจัดรูปแบบ การแยกวิเคราะห์ เวลาสัมพัทธ์ และระยะเวลาของ Day.js กับวันที่ที่คุณเลือก โดยผลลัพธ์อัปเดตขณะพิมพ์ แต่ละผลมาพร้อมบรรทัด import และ dayjs.extend() ที่ต้องใช้ และตารางโทเคนแสดงว่าโทเคนรูปแบบแต่ละตัวต้องใช้ปลั๊กอินใด
- 1
ตั้งวันที่และเวลาอ้างอิง (หรือคลิกตอนนี้) เลือกภาษา และเปิดถือเป็น UTC หากโค้ดของคุณใช้ dayjs.utc()
- 2
พิมพ์รูปแบบในสตริงรูปแบบ หรือคลิกค่าที่ตั้งไว้ คลิกโทเคนใดก็ได้ในโทเคนรูปแบบเพื่อแทรกที่เคอร์เซอร์
- 3
คัดลอกโค้ดที่สร้างใต้ผลลัพธ์ ซึ่งรวมการนำเข้าปลั๊กอินที่ต้องใช้แล้ว
- 4
ในสนามทดลองแยกวิเคราะห์ ใส่สตริงวันที่และรูปแบบที่คาดไว้ แล้วเปิดการแยกวิเคราะห์แบบเข้มงวดเพื่อปฏิเสธวันที่ที่ไม่มีอยู่
- 5
คลิกลองบนสูตรใดก็ได้เพื่อโหลดเข้าตัวรันโค้ด แล้วแก้และดูผลอัปเดต
คำถามที่พบบ่อย
โทเคนรูปแบบต้องใช้ปลั๊กอินใด?
คอลัมน์ Day.js ของโทเคนรูปแบบบอก core หรือชื่อปลั๊กอิน เช่น advancedFormat สำหรับ Do, Q และ X หรือ localizedFormat สำหรับ L และ LLLL ตัวอย่างโค้ดเติมบรรทัด import และ dayjs.extend() ที่ตรงให้คุณ
ทำไมโทเคนถึงใช้ได้ที่นี่แต่ผิดในโปรเจกต์ของฉัน?
สนามทดลองนี้โหลดทุกปลั๊กอิน แกน Day.js แทนที่เฉพาะโทเคนที่รู้จัก ดังนั้นหากไม่มี advancedFormat Q จะยังเป็น Q และ Do จะกลายเป็นเลขวันตามด้วย o ที่หลุดมา ขยายปลั๊กอินที่ตารางระบุ โทเคน Moment บางตัวเช่น DDD หรือ E ไม่มีใน Day.js เลย
ทำไม 2023-02-30 ถึงถูกยอมรับว่าเป็นวันที่ถูกต้อง?
เมื่อไม่มีโหมดเข้มงวด Day.js จะเลื่อนวันที่ที่เป็นไปไม่ได้ไปเดือนถัดไป เปิดการแยกวิเคราะห์แบบเข้มงวด (อาร์กิวเมนต์ที่สาม true พร้อมปลั๊กอิน customParseFormat) แล้วข้อมูลเข้าเดียวกันจะถูกบอกว่าไม่ถูกต้อง
ใส่ข้อความธรรมดาในรูปแบบได้อย่างไร?
ครอบด้วยวงเล็บเหลี่ยม เช่น [Week] W, GGGG จะพิมพ์คำว่า Week ตามด้วยเลขสัปดาห์ ISO และปี
ตัวรันโค้ดส่งโค้ดของฉันไปที่ใดหรือไม่?
ไม่ Day.js ปลั๊กอิน และไฟล์ภาษาทำงานในหน้านี้ และนิพจน์ของคุณถูกประเมินในเบราว์เซอร์ โดยเปิดทุกปลั๊กอินที่สูตรใช้
แปลงเวลาไปเขตเวลาอื่นได้หรือไม่?
สูตรเวลาในเขตเวลาอื่นแสดง dayjs().tz() พร้อมปลั๊กอิน utc และ timezone เพื่อเปรียบเทียบหลายเขตเคียงข้างกัน ใช้ตัวแปลงเขตเวลา