Tanggal dan Waktu JavaScript

Dalam tutorial ini, Anda akan belajar tentang tanggal dan waktu dalam JavaScript dengan bantuan contoh.

Dalam JavaScript, tanggal dan waktu diwakili oleh Dateobjek. The Dateobjek memberikan tanggal dan waktu informasi dan juga menyediakan berbagai metode.

Tanggal JavaScript menentukan periode EcmaScript yang mewakili milidetik sejak 1 Januari 1970 UTC . Tanggal dan waktu ini sama dengan zaman UNIX (nilai dasar utama untuk nilai tanggal dan waktu yang direkam komputer).

Membuat Objek Tanggal

Ada empat cara untuk membuat objek tanggal.

  • Tanggal baru ()
  • Tanggal baru (milidetik)
  • Tanggal baru (string Tanggal)
  • Tanggal baru (tahun, bulan, hari, jam, menit, detik, milidetik)

Tanggal baru ()

Anda dapat membuat objek tanggal menggunakan new Date()konstruktor. Sebagai contoh,

 const timeNow = new Date(); console.log(timeNow); // shows current date and time

Keluaran

 Senin, 06 Juli 2020 12:03:49 GMT + 0545 (Waktu Nepal)

Di sini, new Date()membuat objek tanggal baru dengan tanggal dan waktu lokal saat ini.

Tanggal baru (milidetik)

The Dateobjek berisi nomor yang mewakili milidetik sejak 1 Januari 1970 UTC .

new Date(milliseconds)membuat objek tanggal baru dengan menambahkan milidetik ke waktu nol. Sebagai contoh,

 const time1 = new Date(0); // epoch time console.log(time1); // Thu Jan 01 1970 05:30:00 // 100000000000 milliseconds after the epoch time const time2 = new Date(100000000000) console.log(time2); // Sat Mar 03 1973 15:16:40

Catatan : 1000 milidetik sama dengan 1 detik.

Tanggal baru (string tanggal)

new Date(date string) membuat objek tanggal baru dari string tanggal.

Di JavaScript, umumnya ada tiga format masukan tanggal.

Format Tanggal ISO

Anda dapat membuat objek tanggal dengan meneruskan format tanggal ISO. Sebagai contoh,

 // ISO Date(International Standard) const date = new Date("2020-07-01"); // the result date will be according to UTC console.log(date); // Wed Jul 01 2020 05:45:00 GMT+0545

Anda juga bisa melewatkan hanya tahun dan bulan atau hanya tahun. Sebagai contoh,

 const date = new Date("2020-07"); console.log(date); // Wed Jul 01 2020 05:45:00 GMT+0545 const date1 = new Date("2020"); console.log(date1); // Wed Jul 01 2020 05:45:00 GMT+0545

Anda juga dapat melewatkan waktu tertentu ke tanggal ISO.

 const date = new Date("2020-07-01T12:00:00Z"); console.log(date); // Wed Jul 01 2020 17:45:00 GMT+0545

Catatan : Tanggal dan waktu dipisahkan dengan huruf kapital T . Dan waktu UTC didefinisikan dengan modal Z .

Format tanggal pendek dan panjang

Dua format tanggal lainnya adalah format tanggal pendek dan format tanggal panjang .

 // short date format "MM/DD/YYYY" const date = new Date("03/25/2015"); console.log(date); // Wed Mar 25 2015 00:00:00 GMT+0545 // long date format "MMM DD YYYY" const date1 = new Date("Jul 1 2020"); console.log(date1); // Wed Jul 01 2020 00:00:00 GMT+0545 // month and day can be in any order const date2 = new Date("1 Jul 2020"); console.log(date2); // Wed Jul 01 2020 00:00:00 GMT+0545 // month can be full or abbreviated. Also month names are insensitive. // comma are ignored const date3 = new Date("July 1 2020"); console.log(date3); // Wed Jul 01 2020 00:00:00 GMT+0545 const date4 = new Date("JULY, 1, 2020"); console.log(date4); // Wed Jul 01 2020 00:00:00

Tanggal baru (tahun, bulan, hari, jam, menit, detik, milidetik)

new Date(year, month,… )membuat objek tanggal baru dengan melewatkan tanggal dan waktu tertentu. Sebagai contoh,

 const time1 = new Date(2020, 1, 20, 4, 12, 11, 0); console.log(time1); // Thu Feb 20 2020 04:12:11

Argumen yang lolos memiliki urutan tertentu.

Jika empat angka dilewatkan, itu mewakili tahun, bulan, hari dan jam. Sebagai contoh,

 const time1 = new Date(2020, 1, 20, 4); console.log(time1); // Thu Feb 20 2020 04:00:00

Demikian pula, jika dua argumen dilewatkan, ini mewakili tahun dan bulan. Sebagai contoh,

 const time1 = new Date(2020, 1); console.log(time1); // Sat Feb 01 2020 00:00:00

Catatan : Jika Anda meneruskan hanya satu argumen, ini akan dianggap sebagai milidetik. Karenanya, Anda harus memberikan dua argumen untuk menggunakan format tanggal ini.

Dalam JavaScript, bulan dihitung dari 0 hingga 11 . Januari adalah 0 dan Desember adalah 11 .

Metode Tanggal JavaScript

Ada berbagai metode yang tersedia di objek Tanggal JavaScript.

metode Deskripsi
sekarang() Mengembalikan nilai numerik yang sesuai dengan waktu saat ini (jumlah milidetik yang berlalu sejak 1 Januari 1970 00:00:00 UTC)
getFullYear () Mendapat tahun sesuai waktu setempat
getMonth () Mendapatkan bulan, dari 0 hingga 11 sesuai waktu setempat
getDate () Mendapatkan hari dalam sebulan (1-31) sesuai waktu setempat
getDay () Mendapat hari dalam seminggu (0-6) sesuai waktu setempat
getHours () Mendapat jam dari 0 hingga 23 sesuai waktu setempat
getMinutes Gets the minute from 0 to 59 according to local time
getUTCDate() Gets the day of the month (1-31) according to universal time
setFullYear() Sets the full year according to local time
setMonth() Sets the month according to local time
setDate() Sets the day of the month according to local time
setUTCDate() Sets the day of the month according to universal time

Example: Date Methods

 const timeInMilliseconds = Date.now(); console.log(timeInMilliseconds); // 1593765214488 const time = new Date; // get day of the month const date = time.getDate(); console.log(date); // 30 // get day of the week const year = time.getFullYear(); console.log(year); // 2020 const utcDate = time.getUTCDate(); console.log(utcDate); // 30 const event = new Date('Feb 19, 2020 23:15:30'); // set the date event.setDate(15); console.log(event.getDate()); // 15 // Only 28 days in February! event.setDate(35); console.log(event.getDate()); // 7

Formatting a Date

Unlike other programming languages, JavaScript does not provide a built-in function for formatting a date.

However, you can extract individual bits and use it like this.

 const currentDate = new Date(); const date = currentDate.getDate(); const month = currentDate.getMonth(); const year = currentDate.getFullYear(); // show in specific format let monthDateYear = (month+1) + '/' + date + '/' + year; console.log(monthDateYear); // 7/3/2020

Note: The above program gives inconsistent length as date and month have single-digit and double-digit.

AutoCorrection in Date Object

When you assign out of range values in the Date object, it auto-corrects itself. For example,

 const date = new Date(2008, 0, 33); // Jan does not have 33 days console.log(date);

Output

 Sat Feb 02 2008

Untuk mempelajari lebih lanjut tentang tanggal dan waktu dalam JavaScript, kunjungi Demystifying Tanggal dan Waktu.

Artikel yang menarik...