Playground Moment.js e migrazione a Day.js
- I file non lasciano il dispositivo
- Funziona nel browser
- Gratis, senza account, senza filigrana
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.
Come usare Playground Moment.js e migrazione a Day.js
Prova formattazione, parsing, tempo relativo e durate di Moment.js, con il risultato Day.js mostrato accanto a ogni risultato Moment. Le differenze vengono segnalate, così puoi vedere cosa cambierebbe prima di migrare un codice. Entrambe le librerie girano nel browser.
- 1
Imposta Data e ora di riferimento e una Lingua e attiva Tratta come UTC se il codice usa moment.utc().
- 2
Digita un modello in Stringa di formato; l'Output mostra il risultato di Moment e quello che dà Day.js, contrassegnato come Stesso output o Output diverso.
- 3
Controlla la tabella Token di formato per i token che a Day.js mancano o per cui serve un plugin e fai clic su un token per inserirlo.
- 4
Confronta le due librerie nel Playground di parsing, nel Tempo relativo e nelle Durate, dove i valori Day.js che differiscono sono evidenziati.
- 5
Usa le Ricette per il codice affiancato delle attività di tutti i giorni e fai clic su Prova per eseguire la versione Moment nel Runner del codice.
Domande frequenti
Moment.js è ancora mantenuto?
Moment.js è in modalità di manutenzione e il suo stesso team consiglia una libreria moderna per i progetti nuovi. La nota in alto rimanda alla pagina sullo stato del progetto Moment e al playground di Day.js.
Cosa si rompe di solito passando a Day.js?
La mutabilità: add() di Moment modifica l'oggetto originale, mentre Day.js ne restituisce uno nuovo, quindi i risultati vanno riassegnati. Molte funzioni richiedono anche plugin di Day.js, che ogni ricetta elenca.
Quali token di formato non hanno un equivalente Day.js?
Qo, Mo, DDD, DDDD, e, E, S e SS. La tabella Token di formato li segna come assenti da Day.js e nomina il plugin necessario per gli altri.
Perché i token z e zzz non funzionano in Moment?
I nomi dei fusi orari richiedono il componente aggiuntivo moment-timezone, che questa pagina non carica, quindi la tabella mostra serve moment-timezone. Day.js li fornisce tramite il suo plugin timezone.
Quali versioni vengono confrontate?
Le versioni esatte di Moment.js e Day.js sono mostrate sotto il pannello delle impostazioni.
Il mio codice viene inviato a un server?
No. Entrambe le librerie e i loro file delle lingue si caricano in questa pagina e tutto ciò che digiti viene valutato nel browser.
