JavaScript Dapatkan Metode Tanggal
Metode ini dapat digunakan untuk mendapatkan informasi dari objek tanggal:
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. |
Metode getTime()
Metode getTime()
ini mengembalikan jumlah milidetik sejak 1 Januari 1970:
Contoh
const d = new Date();
d.getTime();
Metode getFullYear()
Metode getFullYear()
ini mengembalikan tahun dari sebuah tanggal sebagai angka empat digit:
Contoh
const d = new Date();
d.getFullYear();
Metode getMonth()
Metode getMonth()
ini mengembalikan bulan dari tanggal sebagai angka (0-11):
Contoh
const d = new Date();
d.getMonth();
Dalam JavaScript, bulan pertama (Januari) adalah bulan nomor 0, jadi Desember mengembalikan bulan nomor 11.
Anda dapat menggunakan array nama, dan getMonth()
untuk mengembalikan bulan sebagai nama:
Contoh
const months = ["January", "February", "March", "April", "May",
"June", "July", "August", "September", "October",
"November", "December"];
const d = new Date();
let month = months[d.getMonth()];
Metode getDate()
Metode getDate()
ini mengembalikan hari dari suatu tanggal sebagai angka (1-31):
Contoh
const d = new Date();
d.getDate();
Metode getHours()
Metode getHours()
ini mengembalikan jam tanggal sebagai angka (0-23):
Contoh
const d = new Date();
d.getHours();
Metode getMinutes()
Metode getMinutes()
ini mengembalikan menit dari suatu tanggal sebagai angka (0-59):
Contoh
const d = new Date();
d.getMinutes();
Metode getSeconds()
Metode getSeconds()
ini mengembalikan detik tanggal sebagai angka (0-59):
Contoh
const d = new Date();
d.getSeconds();
Metode getMilliseconds()
Metode getMilliseconds()
ini mengembalikan milidetik tanggal sebagai angka (0-999):
Contoh
const d = new Date();
d.getMilliseconds();
Metode getDay()
Metode getDay()
ini mengembalikan hari kerja dari suatu tanggal sebagai angka (0-6):
Contoh
const d = new Date();
d.getDay();
Dalam JavaScript, hari pertama dalam seminggu (0) berarti "Minggu", meskipun beberapa negara di dunia menganggap hari pertama dalam seminggu sebagai "Senin"
Anda dapat menggunakan array nama, dan getDay()
untuk mengembalikan hari kerja sebagai nama:
Contoh
const days = ["Sunday", "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday"];
const d = new Date();
let day = days[d.getDay()];
Metode Tanggal UTC
Metode tanggal UTC digunakan untuk bekerja dengan tanggal UTC (Tanggal Zona Waktu Universal):
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 |
Referensi Tanggal JavaScript Lengkap
Untuk referensi Tanggal lengkap, kunjungi:
Referensi Tanggal JavaScript Lengkap .
Referensi berisi deskripsi dan contoh semua properti dan metode Tanggal.