Playground Day.js e riferimento dei formati

Prova in tempo reale formattazione e parsing di Day.js, con tabella dei token, lingue e codice da copiare
  • I file non lasciano il dispositivo
  • Funziona nel browser
  • Gratis, senza account, senza filigrana
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

Come usare Playground Day.js e riferimento dei formati

Prova formattazione, parsing, tempo relativo e durate di Day.js su una data che scegli, con un output che si aggiorna mentre digiti. Ogni risultato arriva con le righe import e dayjs.extend() di cui ha bisogno e una tabella dei token mostra quale plugin richiede ogni token di formato.

  1. 1

    Imposta Data e ora di riferimento (oppure fai clic su Ora), scegli una Lingua e attiva Tratta come UTC se il codice usa dayjs.utc().

  2. 2

    Digita un modello in Stringa di formato o fai clic su un preset; fai clic su un token in Token di formato per inserirlo sul cursore.

  3. 3

    Copia il codice generato sotto l'Output, che include già gli import dei plugin di cui ha bisogno.

  4. 4

    Nel Playground di parsing, inserisci una Stringa data e un Formato atteso e attiva Parsing rigoroso per rifiutare le date che non esistono.

  5. 5

    Fai clic su Prova su una delle Ricette per caricarla nel Runner del codice, poi modificala e guarda il risultato aggiornarsi.

Domande frequenti

Quale plugin mi serve per un token di formato?

La colonna Day.js di Token di formato dice core oppure nomina il plugin, per esempio advancedFormat per Do, Q e X, o localizedFormat per L e LLLL. Lo snippet di codice aggiunge per te le righe import e dayjs.extend() corrispondenti.

Perché un token funziona qui ma nel mio progetto esce sbagliato?

Questo playground carica ogni plugin. Il core di Day.js sostituisce solo i token che conosce, quindi senza advancedFormat Q resta Q e Do diventa il numero del giorno seguito da una o spuria. Estendi il plugin che la tabella nomina; alcuni token di Moment, come DDD o E, in Day.js non esistono affatto.

Perché 2023-02-30 viene accettato come data valida?

Senza la modalità rigorosa, Day.js fa slittare un giorno impossibile nel mese successivo. Attiva Parsing rigoroso (il terzo argomento, true, con il plugin customParseFormat) e lo stesso input viene segnalato come non valido.

Come inserisco testo semplice in un formato?

Avvolgilo tra parentesi quadre. Per esempio, [Week] W, GGGG stampa la parola Week seguita dal numero di settimana ISO e dall'anno.

Il runner del codice invia il mio codice da qualche parte?

No. Day.js, i suoi plugin e i file delle lingue girano dentro questa pagina e l'espressione viene valutata nel browser con ogni plugin usato dalle ricette abilitato.

Posso convertire un'ora in un altro fuso orario?

La ricetta Ora in un altro fuso orario mostra dayjs().tz() con i plugin utc e timezone. Per confrontare più fusi affiancati, usa il Convertitore di fusi orari.