L'oggetto nativo Date in JavaScript rappresenta un singolo momento nel tempo con precisione al millisecondo a partire dal 1 gennaio 1970.
Creazione di Istanze di Data
const now = new Date(); // Momento corrente
const epoch = new Date(1730000000000); // Da timestamp in millisecondi
const iso = new Date('2026-10-25T14:30:00Z'); // Da stringa formattata ISO 8601
const custom = new Date(2026, 9, 25, 14, 30); // Anno, mese (0 e gennaio), giorno, ore, minuti
Metodi di Lettura (Getters)
| Metodo | Ora Locale | Equivalente UTC |
|---|---|---|
getFullYear() |
Anno a 4 cifre | getUTCFullYear() |
getMonth() |
Indice del mese (da 0 a 11) | getUTCMonth() |
getDate() |
Giorno del mese (da 1 a 31) | getUTCDate() |
getDay() |
Giorno della settimana (0 e domenica) | getUTCDay() |
getHours() |
Ore (da 0 a 23) | getUTCHours() |
getMinutes() |
Minuti (da 0 a 59) | getUTCMinutes() |
getSeconds() |
Secondi (da 0 a 59) | getUTCSeconds() |
getMilliseconds() |
Millisecondi (da 0 a 999) | getUTCMilliseconds() |
getTime() |
Timestamp Unix in millisecondi | (same timestamp) |
Operazioni e Differenze Temporali
// Aggiungi 7 giorni alla data
const target = new Date();
target.setDate(target.getDate() + 7);
// Differenza tra due date in giorni
const d1 = new Date('2026-10-01');
const d2 = new Date('2026-10-25');
const diffMs = d2 - d1;
const diffDays = Math.floor(diffMs / (1000 * 60 * 60 * 24)); // => 24
Formattazione e Visualizzazione
const date = new Date('2026-10-25T14:30:00Z');
date.toISOString(); // "2026-10-25T14:30:00.000Z"
date.toDateString(); // "Sun Oct 25 2026"
date.toTimeString(); // "14:30:00 GMT+0000"
date.toLocaleDateString(); // e.g. "10/25/2026"