JavaScript Ottieni metodi di data
Questi metodi possono essere utilizzati per ottenere informazioni da un oggetto data:
Method | Description |
---|---|
getFullYear() | Get the year as a four digit number (yyyy) |
getMonth() | Get the month as a number (0-11) |
getDate() | Get the day as a number (1-31) |
getHours() | Get the hour (0-23) |
getMinutes() | Get the minute (0-59) |
getSeconds() | Get the second (0-59) |
getMilliseconds() | Get the millisecond (0-999) |
getTime() | Get the time (milliseconds since January 1, 1970) |
getDay() | Get the weekday as a number (0-6) |
Date.now() | Get the time. ECMAScript 5. |
Il metodo getTime()
Il getTime()
metodo restituisce il numero di millisecondi dal 1 gennaio 1970:
Esempio
const d = new Date();
d.getTime();
Il metodo getFullYear()
Il getFullYear()
metodo restituisce l'anno di una data come un numero di quattro cifre:
Esempio
const d = new Date();
d.getFullYear();
Il metodo getMonth()
Il getMonth()
metodo restituisce il mese di una data come numero (0-11):
Esempio
const d = new Date();
d.getMonth();
In JavaScript, il primo mese (gennaio) è il mese numero 0, quindi dicembre restituisce il mese numero 11.
Puoi usare una matrice di nomi e getMonth()
per restituire il mese come nome:
Esempio
const months = ["January", "February", "March", "April", "May",
"June", "July", "August", "September", "October",
"November", "December"];
const d = new Date();
let month = months[d.getMonth()];
Il metodo getDate()
Il getDate()
metodo restituisce il giorno di una data come numero (1-31):
Esempio
const d = new Date();
d.getDate();
Il metodo getHours()
Il getHours()
metodo restituisce le ore di una data sotto forma di numero (0-23):
Esempio
const d = new Date();
d.getHours();
Il metodo getMinutes()
Il getMinutes()
metodo restituisce i minuti di una data sotto forma di numero (0-59):
Esempio
const d = new Date();
d.getMinutes();
Il metodo getSeconds()
Il getSeconds()
metodo restituisce i secondi di una data sotto forma di numero (0-59):
Esempio
const d = new Date();
d.getSeconds();
Il metodo getMilliseconds()
Il getMilliseconds()
metodo restituisce i millisecondi di una data sotto forma di numero (0-999):
Esempio
const d = new Date();
d.getMilliseconds();
Il metodo getDay()
Il getDay()
metodo restituisce il giorno della settimana di una data come numero (0-6):
Esempio
const d = new Date();
d.getDay();
In JavaScript, il primo giorno della settimana (0) significa "domenica", anche se alcuni paesi del mondo considerano il primo giorno della settimana "lunedì"
È possibile utilizzare una matrice di nomi e getDay()
restituire il giorno della settimana come nome:
Esempio
const days = ["Sunday", "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday"];
const d = new Date();
let day = days[d.getDay()];
Metodi della data UTC
I metodi della data UTC vengono utilizzati per lavorare con le date UTC (date del fuso orario universale):
Method | Description |
---|---|
getUTCDate() | Same as getDate(), but returns the UTC date |
getUTCDay() | Same as getDay(), but returns the UTC day |
getUTCFullYear() | Same as getFullYear(), but returns the UTC year |
getUTCHours() | Same as getHours(), but returns the UTC hour |
getUTCMilliseconds() | Same as getMilliseconds(), but returns the UTC milliseconds |
getUTCMinutes() | Same as getMinutes(), but returns the UTC minutes |
getUTCMonth() | Same as getMonth(), but returns the UTC month |
getUTCSeconds() | Same as getSeconds(), but returns the UTC seconds |
Riferimento completo della data JavaScript
Per un riferimento completo alla data, vai al nostro:
Riferimento completo della data JavaScript .
Il riferimento contiene descrizioni ed esempi di tutte le proprietà e i metodi di Date.