JavaScript Pobierz metody daty
Te metody mogą być użyte do uzyskania informacji z obiektu daty:
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. |
Metoda getTime()
Metoda getTime()
zwraca liczbę milisekund od 1 stycznia 1970 r.:
Przykład
const d = new Date();
d.getTime();
Metoda getFullYear()
Metoda getFullYear()
zwraca rok daty jako czterocyfrową liczbę:
Przykład
const d = new Date();
d.getFullYear();
Metoda getMonth()
Metoda getMonth()
zwraca miesiąc daty jako liczbę (0-11):
Przykład
const d = new Date();
d.getMonth();
W JavaScript pierwszy miesiąc (styczeń) to miesiąc numer 0, więc grudzień zwraca miesiąc numer 11.
Możesz użyć tablicy nazw i getMonth()
zwrócić miesiąc jako nazwę:
Przykład
const months = ["January", "February", "March", "April", "May",
"June", "July", "August", "September", "October",
"November", "December"];
const d = new Date();
let month = months[d.getMonth()];
Metoda getDate()
Metoda getDate()
zwraca dzień daty jako liczbę (1-31):
Przykład
const d = new Date();
d.getDate();
Metoda getHours()
Metoda getHours()
zwraca godziny daty jako liczbę (0-23):
Przykład
const d = new Date();
d.getHours();
Metoda getMinutes()
Metoda getMinutes()
zwraca minuty daty jako liczbę (0-59):
Przykład
const d = new Date();
d.getMinutes();
Metoda getSeconds()
Metoda getSeconds()
zwraca sekundy daty jako liczbę (0-59):
Przykład
const d = new Date();
d.getSeconds();
Metoda getMilliseconds()
Metoda getMilliseconds()
zwraca milisekundy daty jako liczbę (0-999):
Przykład
const d = new Date();
d.getMilliseconds();
Metoda getDay()
Metoda getDay()
zwraca dzień tygodnia daty jako liczbę (0-6):
Przykład
const d = new Date();
d.getDay();
W JavaScript pierwszy dzień tygodnia (0) oznacza „niedzielę”, nawet jeśli niektóre kraje na świecie uważają, że pierwszy dzień tygodnia to „poniedziałek”
Możesz użyć tablicy nazw i getDay()
zwrócić dzień tygodnia jako nazwę:
Przykład
const days = ["Sunday", "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday"];
const d = new Date();
let day = days[d.getDay()];
Metody daty UTC
Metody daty UTC są używane do pracy z datami UTC (daty uniwersalnej strefy czasowej):
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 |
Pełne odniesienie do daty JavaScript
Aby uzyskać pełne odniesienie do daty, przejdź do naszego:
Pełne odniesienie do daty JavaScript .
Odwołanie zawiera opisy i przykłady wszystkich właściwości i metod Date.