diff --git a/1-js/05-data-types/11-date/1-new-date/solution.md b/1-js/05-data-types/11-date/1-new-date/solution.md index 9bb1d749c..bc5c39b5b 100644 --- a/1-js/05-data-types/11-date/1-new-date/solution.md +++ b/1-js/05-data-types/11-date/1-new-date/solution.md @@ -1,6 +1,6 @@ -The `new Date` constructor uses the local time zone. So the only important thing to remember is that months start from zero. +Konstruktor `new Date` menggunakan zona waktu lokal. Sehingga hal penting yang harus diingat adalah bulan dimulai dari angka 0. -So February has number 1. +Jadi Februari mempunyai angka 1. ```js run let d = new Date(2012, 1, 20, 3, 12); diff --git a/1-js/05-data-types/11-date/1-new-date/task.md b/1-js/05-data-types/11-date/1-new-date/task.md index 1b40d5ac0..f5652c0f1 100644 --- a/1-js/05-data-types/11-date/1-new-date/task.md +++ b/1-js/05-data-types/11-date/1-new-date/task.md @@ -1,9 +1,9 @@ -importance: 5 +Tingkat kepentingan: 5 --- -# Create a date +# Buat sebuah tanggal -Create a `Date` object for the date: Feb 20, 2012, 3:12am. The time zone is local. +Buat sebuah objek `Date` untuk tanggal: Feb 20, 2012, 3:12am. Zona waktu lokal. -Show it using `alert`. +Tampilkan menggunakan `alert`. diff --git a/1-js/05-data-types/11-date/2-get-week-day/_js.view/solution.js b/1-js/05-data-types/11-date/2-get-week-day/_js.view/solution.js index 642c376a9..f52d4fd85 100644 --- a/1-js/05-data-types/11-date/2-get-week-day/_js.view/solution.js +++ b/1-js/05-data-types/11-date/2-get-week-day/_js.view/solution.js @@ -1,5 +1,5 @@ function getWeekDay(date) { - let days = ['SU', 'MO', 'TU', 'WE', 'TH', 'FR', 'SA']; + let days = ['MIN', 'SEN', 'SEL', 'RAB', 'KAM', 'JUM', 'SAB']; return days[date.getDay()]; } diff --git a/1-js/05-data-types/11-date/2-get-week-day/_js.view/test.js b/1-js/05-data-types/11-date/2-get-week-day/_js.view/test.js index 3cdc91830..39c9ab821 100644 --- a/1-js/05-data-types/11-date/2-get-week-day/_js.view/test.js +++ b/1-js/05-data-types/11-date/2-get-week-day/_js.view/test.js @@ -1,29 +1,29 @@ describe("getWeekDay", function() { it("3 January 2014 - friday", function() { - assert.equal(getWeekDay(new Date(2014, 0, 3)), 'FR'); + assert.equal(getWeekDay(new Date(2014, 0, 3)), 'JUM'); }); it("4 January 2014 - saturday", function() { - assert.equal(getWeekDay(new Date(2014, 0, 4)), 'SA'); + assert.equal(getWeekDay(new Date(2014, 0, 4)), 'SAB'); }); it("5 January 2014 - sunday", function() { - assert.equal(getWeekDay(new Date(2014, 0, 5)), 'SU'); + assert.equal(getWeekDay(new Date(2014, 0, 5)), 'MIN'); }); it("6 January 2014 - monday", function() { - assert.equal(getWeekDay(new Date(2014, 0, 6)), 'MO'); + assert.equal(getWeekDay(new Date(2014, 0, 6)), 'SEN'); }); it("7 January 2014 - tuesday", function() { - assert.equal(getWeekDay(new Date(2014, 0, 7)), 'TU'); + assert.equal(getWeekDay(new Date(2014, 0, 7)), 'SEL'); }); it("8 January 2014 - wednesday", function() { - assert.equal(getWeekDay(new Date(2014, 0, 8)), 'WE'); + assert.equal(getWeekDay(new Date(2014, 0, 8)), 'RAB'); }); it("9 January 2014 - thursday", function() { - assert.equal(getWeekDay(new Date(2014, 0, 9)), 'TH'); + assert.equal(getWeekDay(new Date(2014, 0, 9)), 'KAM'); }); }); diff --git a/1-js/05-data-types/11-date/2-get-week-day/solution.md b/1-js/05-data-types/11-date/2-get-week-day/solution.md index 58d75c1c3..9cf5c7aca 100644 --- a/1-js/05-data-types/11-date/2-get-week-day/solution.md +++ b/1-js/05-data-types/11-date/2-get-week-day/solution.md @@ -1,14 +1,14 @@ -The method `date.getDay()` returns the number of the weekday, starting from sunday. +Metode `date.getDay()` mengembalikan angka dari hari dalam satu minggu, dimulai dari Minggu. -Let's make an array of weekdays, so that we can get the proper day name by its number: +Buat array hari dalam seminggu, sehingga kita bisa mendapatkan nama yang sesuai dengan angkanya: ```js run demo function getWeekDay(date) { - let days = ['SU', 'MO', 'TU', 'WE', 'TH', 'FR', 'SA']; + let days = ['MIN', 'SEN', 'SEL', 'RAB', 'KAM', 'JUM', 'SAB']; return days[date.getDay()]; } let date = new Date(2014, 0, 3); // 3 Jan 2014 -alert( getWeekDay(date) ); // FR +alert( getWeekDay(date) ); // JUM ``` diff --git a/1-js/05-data-types/11-date/2-get-week-day/task.md b/1-js/05-data-types/11-date/2-get-week-day/task.md index 5cf31565d..0b940fb4e 100644 --- a/1-js/05-data-types/11-date/2-get-week-day/task.md +++ b/1-js/05-data-types/11-date/2-get-week-day/task.md @@ -1,14 +1,14 @@ -importance: 5 +Tingkat kepentingan: 5 --- -# Show a weekday +# Tampilkan hari dalam satu minggu -Write a function `getWeekDay(date)` to show the weekday in short format: 'MO', 'TU', 'WE', 'TH', 'FR', 'SA', 'SU'. +Tulis sebuah fungsi `getWeekDay(tanggal)` untuk menunjukkan hari dalam format: 'SEN', 'SEL', 'RAB', 'KAM', 'JUM', 'SAB', 'MIN'. -For instance: +Sebagai contoh: ```js no-beautify -let date = new Date(2012, 0, 3); // 3 Jan 2012 -alert( getWeekDay(date) ); // should output "TU" +let tanggal = new Date(2012, 0, 3); // 3 Jan 2012 +alert( getWeekDay(tanggal) ); // harus mengeluarkan "SEL" ``` diff --git a/1-js/05-data-types/11-date/3-weekday/_js.view/solution.js b/1-js/05-data-types/11-date/3-weekday/_js.view/solution.js index fb9e3d2a4..332fd9df6 100644 --- a/1-js/05-data-types/11-date/3-weekday/_js.view/solution.js +++ b/1-js/05-data-types/11-date/3-weekday/_js.view/solution.js @@ -1,10 +1,10 @@ function getLocalDay(date) { - let day = date.getDay(); + let days = date.getDay(); - if (day == 0) { // weekday 0 (sunday) is 7 in european - day = 7; + if (days == 0) { // hari ke-0 (Minggu) adalah hari ke-7 di Eropa + days = 7; } - return day; + return days; } diff --git a/1-js/05-data-types/11-date/3-weekday/_js.view/test.js b/1-js/05-data-types/11-date/3-weekday/_js.view/test.js index 57032154f..bfbcffbd7 100644 --- a/1-js/05-data-types/11-date/3-weekday/_js.view/test.js +++ b/1-js/05-data-types/11-date/3-weekday/_js.view/test.js @@ -1,29 +1,29 @@ -describe("getLocalDay returns the \"european\" weekday", function() { - it("3 January 2014 - friday", function() { +describe("getLocalDay mengembalikan hari sesuai dengan standar di \"eropa\" ", function() { + it("3 January 2014 - Jumat", function() { assert.equal(getLocalDay(new Date(2014, 0, 3)), 5); }); - it("4 January 2014 - saturday", function() { + it("4 January 2014 - Sabtu", function() { assert.equal(getLocalDay(new Date(2014, 0, 4)), 6); }); - it("5 January 2014 - sunday", function() { + it("5 January 2014 - Minggu", function() { assert.equal(getLocalDay(new Date(2014, 0, 5)), 7); }); - it("6 January 2014 - monday", function() { + it("6 January 2014 - Senin", function() { assert.equal(getLocalDay(new Date(2014, 0, 6)), 1); }); - it("7 January 2014 - tuesday", function() { + it("7 January 2014 - Selasa", function() { assert.equal(getLocalDay(new Date(2014, 0, 7)), 2); }); - it("8 January 2014 - wednesday", function() { + it("8 January 2014 - Rabu", function() { assert.equal(getLocalDay(new Date(2014, 0, 8)), 3); }); - it("9 January 2014 - thursday", function() { + it("9 January 2014 - Kamis", function() { assert.equal(getLocalDay(new Date(2014, 0, 9)), 4); }); }); diff --git a/1-js/05-data-types/11-date/3-weekday/task.md b/1-js/05-data-types/11-date/3-weekday/task.md index ba62790cf..e67c2be25 100644 --- a/1-js/05-data-types/11-date/3-weekday/task.md +++ b/1-js/05-data-types/11-date/3-weekday/task.md @@ -1,12 +1,12 @@ -importance: 5 +Tingkat kepentingan: 5 --- -# European weekday +# Hari di Eropa -European countries have days of week starting with Monday (number 1), then Tuesday (number 2) and till Sunday (number 7). Write a function `getLocalDay(date)` that returns the "European" day of week for `date`. +Negara-negara di Eropa mempunyai hari yang dimulai dari Senin (angka 1), kemudian Selasa (angka 2) sampai dengan Minggu (angka 7). Buatlah sebuah fungsi `getLocalDay(date)` yang mengembalikan hari sesuai dengan standar tanggal di Eropa. ```js no-beautify let date = new Date(2012, 0, 3); // 3 Jan 2012 -alert( getLocalDay(date) ); // tuesday, should show 2 +alert( getLocalDay(date) ); // Selasa, seharusnya menunjukkan angka 2 ``` diff --git a/1-js/05-data-types/11-date/4-get-date-ago/_js.view/test.js b/1-js/05-data-types/11-date/4-get-date-ago/_js.view/test.js index 255acffe0..91e8f0921 100644 --- a/1-js/05-data-types/11-date/4-get-date-ago/_js.view/test.js +++ b/1-js/05-data-types/11-date/4-get-date-ago/_js.view/test.js @@ -1,27 +1,27 @@ describe("getDateAgo", function() { - it("1 day before 02.01.2015 -> day 1", function() { + it("1 hari sebelum 02.01.2015 -> tanggal 1", function() { assert.equal(getDateAgo(new Date(2015, 0, 2), 1), 1); }); - it("2 days before 02.01.2015 -> day 31", function() { + it("2 hari sebelum 02.01.2015 -> tanggal 31", function() { assert.equal(getDateAgo(new Date(2015, 0, 2), 2), 31); }); - it("100 days before 02.01.2015 -> day 24", function() { + it("100 hari sebelum 02.01.2015 -> tanggal 24", function() { assert.equal(getDateAgo(new Date(2015, 0, 2), 100), 24); }); - it("365 days before 02.01.2015 -> day 2", function() { + it("365 hari sebelum 02.01.2015 -> tanggal 2", function() { assert.equal(getDateAgo(new Date(2015, 0, 2), 365), 2); }); - it("does not modify the given date", function() { - let date = new Date(2015, 0, 2); - let dateCopy = new Date(date); - getDateAgo(dateCopy, 100); - assert.equal(date.getTime(), dateCopy.getTime()); + it("tidak mengubah tanggal yang diberikan", function() { + let tanggal = new Date(2015, 0, 2); + let tanggalCopy = new Date(tanggal); + getDateAgo(tanggalCopy, 100); + assert.equal(tanggal.getTime(), tanggalCopy.getTime()); }); }); diff --git a/1-js/05-data-types/11-date/4-get-date-ago/solution.md b/1-js/05-data-types/11-date/4-get-date-ago/solution.md index 5c394c100..a6f1a0e75 100644 --- a/1-js/05-data-types/11-date/4-get-date-ago/solution.md +++ b/1-js/05-data-types/11-date/4-get-date-ago/solution.md @@ -1,27 +1,27 @@ -The idea is simple: to substract given number of days from `date`: +Idenya mudah: Kurangi `tanggal` dengan jumlah hari yang diberikan. ```js -function getDateAgo(date, days) { - date.setDate(date.getDate() - days); - return date.getDate(); +function getDateAgo(tanggal, hari) { + tanggal.setDate(tanggal.getDate() - hari); + return tanggal.getDate(); } ``` -...But the function should not change `date`. That's an important thing, because the outer code which gives us the date does not expect it to change. +...Namun fungsi tersebut tidak boleh mengubah `tanggal` yang diberikan. Ini adalah yang terpenting, karena kode di luar yang memberikan kita tanggal tidak mengira tanggal tersebut akan berubah. -To implement it let's clone the date, like this: +Untuk mengimplementasikannya kita akan menduplikasi tanggal tersebut, seperti ini: ```js run demo -function getDateAgo(date, days) { - let dateCopy = new Date(date); +function getDateAgo(tanggal, hari) { + let tanggalCopy = new Date(tanggal); - dateCopy.setDate(date.getDate() - days); - return dateCopy.getDate(); + tanggalCopy.setDate(tanggal.getDate() - hari); + return tanggalCopy.getDate(); } -let date = new Date(2015, 0, 2); +let tanggal = new Date(2015, 0, 2); -alert( getDateAgo(date, 1) ); // 1, (1 Jan 2015) -alert( getDateAgo(date, 2) ); // 31, (31 Dec 2014) -alert( getDateAgo(date, 365) ); // 2, (2 Jan 2014) +alert( getDateAgo(tanggal, 1) ); // 1, (1 Jan 2015) +alert( getDateAgo(tanggal, 2) ); // 31, (31 Des 2014) +alert( getDateAgo(tanggal, 365) ); // 2, (2 Jan 2014) ``` diff --git a/1-js/05-data-types/11-date/4-get-date-ago/task.md b/1-js/05-data-types/11-date/4-get-date-ago/task.md index 058d39c7e..f6f825ee7 100644 --- a/1-js/05-data-types/11-date/4-get-date-ago/task.md +++ b/1-js/05-data-types/11-date/4-get-date-ago/task.md @@ -1,21 +1,21 @@ -importance: 4 +Tingkat kepentingan: 4 --- -# Which day of month was many days ago? +# Tanggal berapakah beberapa hari yang lalu? -Create a function `getDateAgo(date, days)` to return the day of month `days` ago from the `date`. +Buatlah sebuah fungsi `getDateAgo(tanggal, hari)` yang mengembalikan beberapa `hari` yang telah berlalu dari sebuah `tanggal`. -For instance, if today is 20th, then `getDateAgo(new Date(), 1)` should be 19th and `getDateAgo(new Date(), 2)` should be 18th. +Sebagai contoh, apabila hari ini tanggal 20, maka `getDateAgo(new Date(), 1)` harus mengembalikan tanggal 19 dan `getDateAgo(new Date(), 2)` harus mengembalikan tanggal 18. -Should work reliably for `days=365` or more: +Harus bekerja dengan baik dan dapat diandalkan untuk `hari=365` atau lebih: ```js -let date = new Date(2015, 0, 2); +let tanggal = new Date(2015, 0, 2); -alert( getDateAgo(date, 1) ); // 1, (1 Jan 2015) -alert( getDateAgo(date, 2) ); // 31, (31 Dec 2014) -alert( getDateAgo(date, 365) ); // 2, (2 Jan 2014) +alert( getDateAgo(tanggal, 1) ); // 1, (1 Jan 2015) +alert( getDateAgo(tanggal, 2) ); // 31, (31 Des 2014) +alert( getDateAgo(tanggal, 365) ); // 2, (2 Jan 2014) ``` -P.S. The function should not modify the given `date`. +P.S. Fungsi tidak boleh mengubah `tanggal` yang diberikan. diff --git a/1-js/05-data-types/11-date/5-last-day-of-month/_js.view/solution.js b/1-js/05-data-types/11-date/5-last-day-of-month/_js.view/solution.js index d0d7d37ff..74a5acc24 100644 --- a/1-js/05-data-types/11-date/5-last-day-of-month/_js.view/solution.js +++ b/1-js/05-data-types/11-date/5-last-day-of-month/_js.view/solution.js @@ -1,4 +1,4 @@ -function getLastDayOfMonth(year, month) { - let date = new Date(year, month + 1, 0); - return date.getDate(); +function getLastDayOfMonth(tahun, bulan) { + let tanggal = new Date(tahun, bulan + 1, 0); + return tanggal.getDate(); } diff --git a/1-js/05-data-types/11-date/5-last-day-of-month/_js.view/test.js b/1-js/05-data-types/11-date/5-last-day-of-month/_js.view/test.js index 4ff3e116a..89c56c5b7 100644 --- a/1-js/05-data-types/11-date/5-last-day-of-month/_js.view/test.js +++ b/1-js/05-data-types/11-date/5-last-day-of-month/_js.view/test.js @@ -1,13 +1,13 @@ describe("getLastDayOfMonth", function() { - it("last day of 01.01.2012 - 31", function() { + it("tanggal terakhir dari 01.01.2012 - 31", function() { assert.equal(getLastDayOfMonth(2012, 0), 31); }); - it("last day of 01.02.2012 - 29 (leap year)", function() { + it("tanggal terakhir dari 01.02.2012 - 29 (tahun kabisat)", function() { assert.equal(getLastDayOfMonth(2012, 1), 29); }); - it("last day of 01.02.2013 - 28", function() { + it("tanggal terakhir dari 01.02.2013 - 28", function() { assert.equal(getLastDayOfMonth(2013, 1), 28); }); }); diff --git a/1-js/05-data-types/11-date/5-last-day-of-month/solution.md b/1-js/05-data-types/11-date/5-last-day-of-month/solution.md index 4f642536e..adb5b025e 100644 --- a/1-js/05-data-types/11-date/5-last-day-of-month/solution.md +++ b/1-js/05-data-types/11-date/5-last-day-of-month/solution.md @@ -1,8 +1,8 @@ -Let's create a date using the next month, but pass zero as the day: +Buat sebuah tanggal menggunakan bulan berikutnya, namun berikan 0 sebagai tanggalnya: ```js run demo -function getLastDayOfMonth(year, month) { - let date = new Date(year, month + 1, 0); - return date.getDate(); +function getLastDayOfMonth(tahun, bulan) { + let tanggal = new Date(tahun, bulan + 1, 0); + return tanggal.getDate(); } alert( getLastDayOfMonth(2012, 0) ); // 31 @@ -10,4 +10,4 @@ alert( getLastDayOfMonth(2012, 1) ); // 29 alert( getLastDayOfMonth(2013, 1) ); // 28 ``` -Normally, dates start from 1, but technically we can pass any number, the date will autoadjust itself. So when we pass 0, then it means "one day before 1st day of the month", in other words: "the last day of the previous month". +Normalnya, tanggal dimulai dari 1, namun secara teknis kita bisa berikan angka apapun, dan tanggal akan otomatis menyesuaikan. Sehingga ketika kita berikan 0, itu berarti "satu hari sebelum tanggal pertama untuk sebuah bulan", dengan kata lain: "hari terakhir pada bulan sebelumnya". diff --git a/1-js/05-data-types/11-date/5-last-day-of-month/task.md b/1-js/05-data-types/11-date/5-last-day-of-month/task.md index 10dfb7a7a..85cd8bd1d 100644 --- a/1-js/05-data-types/11-date/5-last-day-of-month/task.md +++ b/1-js/05-data-types/11-date/5-last-day-of-month/task.md @@ -1,14 +1,14 @@ -importance: 5 +Tingkat kepentingan: 5 --- -# Last day of month? +# Tanggal terakhir dari sebuah bulan? -Write a function `getLastDayOfMonth(year, month)` that returns the last day of month. Sometimes it is 30th, 31st or even 28/29th for Feb. +Tulis sebuah fungsi `getLastDayOfMonth(tahun, bulan)` yang mengembalikan tanggal terakhir dari sebuah bulan. Terkadang 30, 31, atau bahkan 28/29 untuk Februari. -Parameters: +Parameter: -- `year` -- four-digits year, for instance 2012. -- `month` -- month, from 0 to 11. +- `tahun` -- tahun dalam empat-digit, sebagai contoh 2012. +- `bulan` -- bulan, dari 0 sampai 11. -For instance, `getLastDayOfMonth(2012, 1) = 29` (leap year, Feb). +Sebagai contoh, `getLastDayOfMonth(2012, 1) = 29` (tahun kabisat, Feb). diff --git a/1-js/05-data-types/11-date/6-get-seconds-today/solution.md b/1-js/05-data-types/11-date/6-get-seconds-today/solution.md index a483afe93..da5edcfdb 100644 --- a/1-js/05-data-types/11-date/6-get-seconds-today/solution.md +++ b/1-js/05-data-types/11-date/6-get-seconds-today/solution.md @@ -1,22 +1,22 @@ -To get the number of seconds, we can generate a date using the current day and time 00:00:00, then substract it from "now". +Untuk mendapatkan jumlah detik, kita harus membuat sebuah tanggal menggunakan hari yang sedang berlangsung dan waktu 00:00:00, dan mengurangi waktu "saat ini" dengannya. -The difference is the number of milliseconds from the beginning of the day, that we should divide by 1000 to get seconds: +Perbedaan yang didapat adalah angka dalam milidetik sejak permulaan hari, yang harus dibagi dengan 1000 agar menjadi detik: ```js run function getSecondsToday() { - let now = new Date(); + let sekarang = new Date(); - // create an object using the current day/month/year - let today = new Date(now.getFullYear(), now.getMonth(), now.getDate()); + // Buat sebuah objek menggunakan hari/bulan/tahun saat ini + let hariIni = new Date(now.getFullYear(), now.getMonth(), now.getDate()); - let diff = now - today; // ms difference - return Math.round(diff / 1000); // make seconds + let beda = sekarang - hariIni; // beda dalam milidetik + return Math.round(beda / 1000); // ubah menjadi detik } alert( getSecondsToday() ); ``` -An alternative solution would be to get hours/minutes/seconds and convert them to seconds: +Solusi alternatif adalah cari jam/menit/detik dan ubah menjadi detik: ```js run function getSecondsToday() { diff --git a/1-js/05-data-types/11-date/6-get-seconds-today/task.md b/1-js/05-data-types/11-date/6-get-seconds-today/task.md index 456790928..202aeb44b 100644 --- a/1-js/05-data-types/11-date/6-get-seconds-today/task.md +++ b/1-js/05-data-types/11-date/6-get-seconds-today/task.md @@ -1,15 +1,15 @@ -importance: 5 +Tingkat kepentingan: 5 --- -# How many seconds have passed today? +# Berapa detik yang telah berlalu untuk hari ini? -Write a function `getSecondsToday()` that returns the number of seconds from the beginning of today. +Tulis sebuah fungsi `getSecondsToday()` yang mengembalikan angka dari detik yang telah berlalu dari sejak permulaan hari ini. -For instance, if now were `10:00 am`, and there was no daylight savings shift, then: +Sebagai contoh, sekarang pukul `10:00 am`, dan tidak ada daylight savings shift, maka: ```js getSecondsToday() == 36000 // (3600 * 10) ``` -The function should work in any day. That is, it should not have a hard-coded value of "today". +Fungsi haruslah berjalan untuk hari apapun. sehingga, tidak boleh ada nilai "hari ini" yang ditulis secara hard-code. diff --git a/1-js/05-data-types/11-date/7-get-seconds-to-tomorrow/solution.md b/1-js/05-data-types/11-date/7-get-seconds-to-tomorrow/solution.md index c337d1199..4fb67e537 100644 --- a/1-js/05-data-types/11-date/7-get-seconds-to-tomorrow/solution.md +++ b/1-js/05-data-types/11-date/7-get-seconds-to-tomorrow/solution.md @@ -1,32 +1,32 @@ -To get the number of milliseconds till tomorrow, we can from "tomorrow 00:00:00" substract the current date. +Untuk mendapatkan jumlah milidetik hingga besok, kita bisa mendapatkannya melalui pengurangan tanggal hari ini dengan "besok 00:00:00" -First, we generate that "tomorrow", and then do it: +Pertama, kita tentukan "besok", dan kemudian lakukan perhitungan: ```js run function getSecondsToTomorrow() { - let now = new Date(); + let sekarang = new Date(); - // tomorrow date - let tomorrow = new Date(now.getFullYear(), now.getMonth(), *!*now.getDate()+1*/!*); + // besok + let besok = new Date(now.getFullYear(), now.getMonth(), *!*now.getDate()+1*/!*); - let diff = tomorrow - now; // difference in ms - return Math.round(diff / 1000); // convert to seconds + let beda = besok - sekarang; // beda dalam milidetik + return Math.round(beda / 1000); // ubah ke detik } ``` -Alternative solution: +Solusi alternatif: ```js run function getSecondsToTomorrow() { - let now = new Date(); - let hour = now.getHours(); - let minutes = now.getMinutes(); - let seconds = now.getSeconds(); - let totalSecondsToday = (hour * 60 + minutes) * 60 + seconds; - let totalSecondsInADay = 86400; - - return totalSecondsInADay - totalSecondsToday; + let sekarang = new Date(); + let jam = sekarang.getHours(); + let menit = sekarang.getMinutes(); + let detik = sekarang.getSeconds(); + let detikTotalHariIni = (jam * 60 + menit) * 60 + detik; + let detikTotalDalamSatuHari = 86400; + + return detikTotalDalamSatuHari - detikTotalHariIni; } ``` -Please note that many countries have Daylight Savings Time (DST), so there may be days with 23 or 25 hours. We may want to treat such days separately. +Harap diingat bahwa banyak negara menerapkan Daylight Savings Time (DST), sehingga memungkinkan ada hari dengan 23 atau 25 jam. Kita mungkin ingin melakukan perhitungan dengan cara yang berbeda untuk mereka. diff --git a/1-js/05-data-types/11-date/7-get-seconds-to-tomorrow/task.md b/1-js/05-data-types/11-date/7-get-seconds-to-tomorrow/task.md index e05903026..a2ff9ceab 100644 --- a/1-js/05-data-types/11-date/7-get-seconds-to-tomorrow/task.md +++ b/1-js/05-data-types/11-date/7-get-seconds-to-tomorrow/task.md @@ -1,15 +1,15 @@ -importance: 5 +Tingkat kepentingan: 5 --- -# How many seconds till tomorrow? +# Berapa detik lagi sampai besok? -Create a function `getSecondsToTomorrow()` that returns the number of seconds till tomorrow. +Buat sebuah fungsi `getSecondsToTomorrow()` yang mengembalikan jumlah detik yang tersisa hingga esok hari. -For instance, if now is `23:00`, then: +Sebagai contoh, jika sekarang `23:00`, maka: ```js getSecondsToTomorrow() == 3600 ``` -P.S. The function should work at any day, the "today" is not hardcoded. +P.S. Fungsi haruslah berjalan untuk semua hari, sehingga "hari ini" tidak bisa dalam bentuk hard-code. diff --git a/1-js/05-data-types/11-date/8-format-date-relative/solution.md b/1-js/05-data-types/11-date/8-format-date-relative/solution.md index 718618528..e69d4fd15 100644 --- a/1-js/05-data-types/11-date/8-format-date-relative/solution.md +++ b/1-js/05-data-types/11-date/8-format-date-relative/solution.md @@ -1,78 +1,79 @@ -To get the time from `date` till now -- let's substract the dates. +untuk mendapatkan waktu dari `tanggal` hingga saat ini -- Mari kita kurangi tanggal tersebut. ```js run demo -function formatDate(date) { - let diff = new Date() - date; // the difference in milliseconds +function formatTanggal(tanggal) { + let beda = new Date() - tanggal; // beda dalam milidetik - if (diff < 1000) { // less than 1 second - return 'right now'; + if (beda < 1000) { // kurang dari 1 detik + return 'sekarang'; } - let sec = Math.floor(diff / 1000); // convert diff to seconds + let detik = Math.floor(beda / 1000); // ubah beda menjadi detik - if (sec < 60) { - return sec + ' sec. ago'; + if (detik < 60) { + return detik + ' detik lalu'; } - let min = Math.floor(diff / 60000); // convert diff to minutes - if (min < 60) { - return min + ' min. ago'; + let menit = Math.floor(beda / 60000); // ubah beda menjadi menit + if (menit < 60) { + return menit + ' menit lalu'; } - // format the date + // format tanggal // add leading zeroes to single-digit day/month/hours/minutes - let d = date; + // tambahkan awalan 0 untuk hari/bulan/jam/menit dalam apabila 1 digit. + let d = tanggal; d = [ '0' + d.getDate(), '0' + (d.getMonth() + 1), '' + d.getFullYear(), '0' + d.getHours(), '0' + d.getMinutes() - ].map(component => component.slice(-2)); // take last 2 digits of every component + ].map(komponen => komponen.slice(-2)); // ambil 2 digit terakhir untuk setiap komponen - // join the components into date + // gabungkan semua komponen menjadi tanggal return d.slice(0, 3).join('.') + ' ' + d.slice(3).join(':'); } -alert( formatDate(new Date(new Date - 1)) ); // "right now" +alert( formatTanggal(new Date(new Date - 1)) ); // "sekarang" -alert( formatDate(new Date(new Date - 30 * 1000)) ); // "30 sec. ago" +alert( formatTanggal(new Date(new Date - 30 * 1000)) ); // "30 detik lalu" -alert( formatDate(new Date(new Date - 5 * 60 * 1000)) ); // "5 min. ago" +alert( formatTanggal(new Date(new Date - 5 * 60 * 1000)) ); // "5 menit lalu" -// yesterday's date like 31.12.2016, 20:00 -alert( formatDate(new Date(new Date - 86400 * 1000)) ); +// tanggal kemarin seperti 31.12.2016, 20:00 +alert( formatTanggal(new Date(new Date - 86400 * 1000)) ); ``` -Alternative solution: +Solusi alternatif: ```js run -function formatDate(date) { - let dayOfMonth = date.getDate(); - let month = date.getMonth() + 1; - let year = date.getFullYear(); - let hour = date.getHours(); - let minutes = date.getMinutes(); - let diffMs = new Date() - date; - let diffSec = Math.round(diffMs / 1000); - let diffMin = diffSec / 60; - let diffHour = diffMin / 60; +function formatTanggal(tanggal) { + let hariDalamBulan = tanggal.getDate(); + let bulan = tanggal.getMonth() + 1; + let tahun = tanggal.getFullYear(); + let jam = tanggal.getHours(); + let menit = tanggal.getMinutes(); + let bedaMs = new Date() - tanggal; + let bedaDetik = Math.round(bedaMs / 1000); + let bedaMenit = bedaDetik / 60; + let bedaJam = bedaMenit / 60; // formatting - year = year.toString().slice(-2); - month = month < 10 ? '0' + month : month; - dayOfMonth = dayOfMonth < 10 ? '0' + dayOfMonth : dayOfMonth; - hour = hour < 10 ? '0' + hour : hour; - minutes = minutes < 10 ? '0' + minutes : minutes; + tahun = tahun.toString().slice(-2); + bulan = bulan < 10 ? '0' + bulan : bulan; + hariDalamBulan = hariDalamBulan < 10 ? '0' + hariDalamBulan : hariDalamBulan; + jam = jam < 10 ? '0' + jam : jam; + menit = menit < 10 ? '0' + menit : menit; - if (diffSec < 1) { - return 'right now'; - } else if (diffMin < 1) { - return `${diffSec} sec. ago` - } else if (diffHour < 1) { - return `${diffMin} min. ago` + if (bedaDetik < 1) { + return 'sekarang'; + } else if (bedaMenit < 1) { + return `${bedaDetik} detik lalu` + } else if (bedaJam < 1) { + return `${bedaMenit} menit lalu` } else { - return `${dayOfMonth}.${month}.${year} ${hour}:${minutes}` + return `${hariDalamBulan}.${bulan}.${tahun} ${jam}:${menit}` } } ``` diff --git a/1-js/05-data-types/11-date/8-format-date-relative/task.md b/1-js/05-data-types/11-date/8-format-date-relative/task.md index 4dc067375..795ac98d2 100644 --- a/1-js/05-data-types/11-date/8-format-date-relative/task.md +++ b/1-js/05-data-types/11-date/8-format-date-relative/task.md @@ -1,25 +1,25 @@ -importance: 4 +Tingkat kepentingan: 4 --- -# Format the relative date +# Format tanggal relatif -Write a function `formatDate(date)` that should format `date` as follows: +Tulis sebuah fungsi `formatDate(tanggal)` yang akan mengubah `tanggal` sebagai berikut: -- If since `date` passed less than 1 second, then `"right now"`. -- Otherwise, if since `date` passed less than 1 minute, then `"n sec. ago"`. -- Otherwise, if less than an hour, then `"m min. ago"`. -- Otherwise, the full date in the format `"DD.MM.YY HH:mm"`. That is: `"day.month.year hours:minutes"`, all in 2-digit format, e.g. `31.12.16 10:00`. +- Jika sejak `tanggal` berlalu kurang dari 1 detik, maka `"sekarang"`. +- Jika tidak, jika sejak `tanggal` berlalu kurang dari 1 menit, maka `"n menit lalu"`. +- Jika tidak, jika kurang dari 1 jam, maka` "m jam lalu"`. +- Jika tidak, maka tunjukkan tanggal secara penuh dalam format date in the format `"DD.MM.YY HH:mm"`. Yaitu: `"hari.bulan.tahun jam:menit"`, semua dalam format 2-digit, e.g. `31.12.16 10:00`. -For instance: +Sebagai contoh: ```js -alert( formatDate(new Date(new Date - 1)) ); // "right now" +alert( formatTanggal(new Date(new Date - 1)) ); // "sekarang" -alert( formatDate(new Date(new Date - 30 * 1000)) ); // "30 sec. ago" +alert( formatTanggal(new Date(new Date - 30 * 1000)) ); // "30 detik lalu" -alert( formatDate(new Date(new Date - 5 * 60 * 1000)) ); // "5 min. ago" +alert( formatTanggal(new Date(new Date - 5 * 60 * 1000)) ); // "5 menit lalu" -// yesterday's date like 31.12.16, 20:00 -alert( formatDate(new Date(new Date - 86400 * 1000)) ); +// tanggal kemarin seperti 31.12.16, 20:00 +alert( formatTanggal(new Date(new Date - 86400 * 1000)) ); ``` diff --git a/1-js/05-data-types/11-date/article.md b/1-js/05-data-types/11-date/article.md index 6f52a0d7c..b66147f3f 100644 --- a/1-js/05-data-types/11-date/article.md +++ b/1-js/05-data-types/11-date/article.md @@ -1,141 +1,143 @@ -# Date and time +# Tanggal dan waktu -Let's meet a new built-in object: [Date](mdn:js/Date). It stores the date, time and provides methods for date/time management. +Ayo berkenalan dengan objek *built-in* terbaru: [Date](mdn:js/Date). Digunakan untuk menyimpan tanggal, waktu, dan menyediakan metode untuk mengatur tanggal dan waktu. -For instance, we can use it to store creation/modification times, to measure time, or just to print out the current date. +Sebagai contoh, kita bisa menggunakannya untuk menyimpan pembuatan/perubahan waktu, menghitung waktu, atau untuk menunjukkan tanggal yang sedang berlangsung. -## Creation +## Pembuatan -To create a new `Date` object call `new Date()` with one of the following arguments: +Untuk membuat objek `Date` baru, panggil `new Date()` dengan menggunakan salah satu argumen berikut: `new Date()` -: Without arguments -- create a `Date` object for the current date and time: +: Tanpa argumen -- membuat sebuah objek `Date` menggunakan tanggal dan waktu yang sedang berlangsung: ```js run let now = new Date(); - alert( now ); // shows current date/time + alert( now ); // menunjukkan tanggal/waktu yang sedang berlangsung ``` -`new Date(milliseconds)` -: Create a `Date` object with the time equal to number of milliseconds (1/1000 of a second) passed after the Jan 1st of 1970 UTC+0. +`new Date(milidetik)` +: Membuat objek `Date` dengan waktu yang sama dengan jumlah milidetik (1/1000 detik) yang telah berlalu setelah 1 Januari 1970 UTC+0. ```js run - // 0 means 01.01.1970 UTC+0 + // 0 berarti 01.01.1970 UTC+0 let Jan01_1970 = new Date(0); alert( Jan01_1970 ); - // now add 24 hours, get 02.01.1970 UTC+0 + // Sekarang tambahkan 24 jam, menjadi 02.01.1970 UTC+0 let Jan02_1970 = new Date(24 * 3600 * 1000); alert( Jan02_1970 ); ``` - An integer number representing the number of milliseconds that has passed since the beginning of 1970 is called a *timestamp*. + Sebuah angka integer mewakili jumlah milidetik yang telah berlalu sejak awal 1970 dinamakan *timestamp*. - It's a lightweight numeric representation of a date. We can always create a date from a timestamp using `new Date(timestamp)` and convert the existing `Date` object to a timestamp using the `date.getTime()` method (see below). + Merupakan perwakilan angka yang ringan untuk sebuah tanggal. Kita dapat membuat tanggal dari timestamp menggunakan `new Date(timestamp)` dan merubah objek `Date` yang telah ada menjadi timestamp menggunakan metode `date.getTime()` (lihat di bawah). - Dates before 01.01.1970 have negative timestamps, e.g.: + Tanggal sebelum 01.01.1970 mempunyai nilai timestamp negatif, e.g.: + ```js run - // 31 Dec 1969 + // 31 Des 1969 let Dec31_1969 = new Date(-24 * 3600 * 1000); alert( Dec31_1969 ); ``` `new Date(datestring)` -: If there is a single argument, and it's a string, then it is parsed automatically. The algorithm is the same as `Date.parse` uses, we'll cover it later. +: Apabila mempunyai satu argumen, dan merupakan sebuah string, maka akan diurai secara otomatis. Menggunakan algoritma yang sama dengan `Date.parse`, yang akan kita bahas nanti. ```js run let date = new Date("2017-01-26"); alert(date); - // The time is not set, so it's assumed to be midnight GMT and - // is adjusted according to the timezone the code is run in - // So the result could be - // Thu Jan 26 2017 11:00:00 GMT+1100 (Australian Eastern Daylight Time) - // or - // Wed Jan 25 2017 16:00:00 GMT-0800 (Pacific Standard Time) + // Waktu tidak ditentukan, sehingga diasumsikan menjadi tengah malam GMT dan + // disesuaikan dengan zona waktu dimana kode ini berjalan + // sehingga hasilnya dapat menjadi + // Thu Jan 26 2017 07:00:00 GMT+0700 (Western Indonesia Time) + // atau + // Wed Jan 25 2017 04:00:00 GMT-0800 (Pacific Standard Time) ``` -`new Date(year, month, date, hours, minutes, seconds, ms)` -: Create the date with the given components in the local time zone. Only the first two arguments are obligatory. +`new Date(tahun, bulan, tanggal, jam, menit, detik, ms)` +: Membuat tanggal dari komponen yang diberikan di zona waktu lokal. Hanya dua argumen pertama yang wajib diberikan. - - The `year` must have 4 digits: `2013` is okay, `98` is not. - - The `month` count starts with `0` (Jan), up to `11` (Dec). - - The `date` parameter is actually the day of month, if absent then `1` is assumed. - - If `hours/minutes/seconds/ms` is absent, they are assumed to be equal `0`. + - `tahun` harus terdiri dari 4 digit: `2013` boleh, `98` tidak boleh. + - `bulan` dihitung mulai dari `0` (Jan), sampai `11` (Des). + - Parameter `tanggal` merupakan hari pada bulan, apabila kosong akan diasumsikan sebagai `1`. + - Apabila `jam/menit/detik/ms` kosong, maka akan diasumsikan sebagai `0`. - For instance: + Sebagai contoh: ```js new Date(2011, 0, 1, 0, 0, 0, 0); // 1 Jan 2011, 00:00:00 - new Date(2011, 0, 1); // the same, hours etc are 0 by default + new Date(2011, 0, 1); // Sama seperti contoh di atas, jam dan lainnya akan 0 secara default ``` - The minimal precision is 1 ms (1/1000 sec): + Presisi minimal adalah 1 ms (1/1000 detik): ```js run let date = new Date(2011, 0, 1, 2, 3, 4, 567); alert( date ); // 1.01.2011, 02:03:04.567 ``` -## Access date components +## Mengakses komponen tanggal -There are methods to access the year, month and so on from the `Date` object: +Ada beberapa metode untuk mengakses tahun, bulan dan sebagainya dari objek `Date`: [getFullYear()](mdn:js/Date/getFullYear) -: Get the year (4 digits) +: Mendapatkan nilai tahun (4 digit) [getMonth()](mdn:js/Date/getMonth) -: Get the month, **from 0 to 11**. +: Mendapatkan nilai bulan, **dari 0 sampai 11**. [getDate()](mdn:js/Date/getDate) -: Get the day of month, from 1 to 31, the name of the method does look a little bit strange. +: Mendapatkan nilai hari dari satu bulan, dari 1 sampai 31, nama dari metodenya memang terlihat sedikit aneh. [getHours()](mdn:js/Date/getHours), [getMinutes()](mdn:js/Date/getMinutes), [getSeconds()](mdn:js/Date/getSeconds), [getMilliseconds()](mdn:js/Date/getMilliseconds) -: Get the corresponding time components. +: Mendapatkan nilai komponen waktu yang sesuai. -```warn header="Not `getYear()`, but `getFullYear()`" -Many JavaScript engines implement a non-standard method `getYear()`. This method is deprecated. It returns 2-digit year sometimes. Please never use it. There is `getFullYear()` for the year. +```warn header="Bukan `getYear()`, tapi `getFullYear()`" +Banyak mesin JavaScript mengimplementasikan metode non-standar `getYear()`. Metode ini sudah usang. Terkadang mengembalikan nilai tahun sebagai 2-digit. Ada `getFullYear()` untuk tahun. ``` -Additionally, we can get a day of week: +Sebagai tambahan, kita dapat mengambil nilai hari dari satu minggu: [getDay()](mdn:js/Date/getDay) -: Get the day of week, from `0` (Sunday) to `6` (Saturday). The first day is always Sunday, in some countries that's not so, but can't be changed. +: Mendapatkan hari dari satu minggu, dari `0` (Minggu) sampai `6` (Sabtu). Hari pertama selalu Minggu, di beberapa negara tidak begitu namun tidak bisa diubah. -**All the methods above return the components relative to the local time zone.** +**Semua metode di atas mengembalikan komponen relatif dengan zona waktu lokal** -There are also their UTC-counterparts, that return day, month, year and so on for the time zone UTC+0: [getUTCFullYear()](mdn:js/Date/getUTCFullYear), [getUTCMonth()](mdn:js/Date/getUTCMonth), [getUTCDay()](mdn:js/Date/getUTCDay). Just insert the `"UTC"` right after `"get"`. +Dan ada juga metode pasangan untuk UTC, yang mengembalikan hari, bulan, tahun dan sebagainya untuk zona waktu UTC+0: [getUTCFullYear()](mdn:js/Date/getUTCFullYear), [getUTCMonth()](mdn:js/Date/getUTCMonth), [getUTCDay()](mdn:js/Date/getUTCDay). Hanya masukkan `"UTC"` tepat setelah `"get"`. -If your local time zone is shifted relative to UTC, then the code below shows different hours: +Apabila zona waktu lokal anda bergeser relatif dengan UTC, maka kode di bawah akan menunjukkan jam yang berbeda: ```js run -// current date +// tanggal yang sedang berlangsung let date = new Date(); -// the hour in your current time zone +// jam yang sedang berlangsung pada zona waktu anda alert( date.getHours() ); -// the hour in UTC+0 time zone (London time without daylight savings) +// jam pada zona waktu UTC+0 (waktu London tanpa daylight savings) alert( date.getUTCHours() ); ``` -Besides the given methods, there are two special ones that do not have a UTC-variant: +Selain metode yang diberikan, ada dua metode spesial yang tidak mempunyai varian untuk UTC: [getTime()](mdn:js/Date/getTime) -: Returns the timestamp for the date -- a number of milliseconds passed from the January 1st of 1970 UTC+0. +: Mengembalikan timestamp untuk tanggal -- sejumlah angka dalam milidetik yang telah berlalu sejak 1 Januari 1970 UTC+0. [getTimezoneOffset()](mdn:js/Date/getTimezoneOffset) -: Returns the difference between the local time zone and UTC, in minutes: +: Mengembalikan perbedaan antara zona waktu lokal dengan UTC dalam hitungan menit: ```js run - // if you are in timezone UTC-1, outputs 60 - // if you are in timezone UTC+3, outputs -180 + // jika anda dalam zona waktu UTC-1, mengeluarkan output 60 + // jika anda dalam zona waktu UTC+3, mengeluarkan output -180 alert( new Date().getTimezoneOffset() ); ``` -## Setting date components +## Pengaturan komponen tanggal The following methods allow to set date/time components: +Metode-metode berikut ini dapat digunakan untuk mengatur komponen tanggal/waktu: - [`setFullYear(year, [month], [date])`](mdn:js/Date/setFullYear) - [`setMonth(month, [date])`](mdn:js/Date/setMonth) @@ -144,38 +146,38 @@ The following methods allow to set date/time components: - [`setMinutes(min, [sec], [ms])`](mdn:js/Date/setMinutes) - [`setSeconds(sec, [ms])`](mdn:js/Date/setSeconds) - [`setMilliseconds(ms)`](mdn:js/Date/setMilliseconds) -- [`setTime(milliseconds)`](mdn:js/Date/setTime) (sets the whole date by milliseconds since 01.01.1970 UTC) +- [`setTime(milliseconds)`](mdn:js/Date/setTime) (mengatur seluruh set tanggal dalam milidetik sejak 01.01.1970 UTC) -Every one of them except `setTime()` has a UTC-variant, for instance: `setUTCHours()`. +Setiap metode selain `setTime()` mempunyai varian UTC, sebagai contoh: `setUTCHours()`. -As we can see, some methods can set multiple components at once, for example `setHours`. The components that are not mentioned are not modified. +Seperti yang kita lihat, beberapa metode dapat mengatur beberapa komponen sekaligus, contohnya `setHours`. Komponen yang tidak disebutkan berarti tidak dimodifikasi. -For instance: +Sebagai contoh: ```js run let today = new Date(); today.setHours(0); -alert(today); // still today, but the hour is changed to 0 +alert(today); // masih today, tapi jam berubah menjadi 0. today.setHours(0, 0, 0, 0); -alert(today); // still today, now 00:00:00 sharp. +alert(today); // masih today, sekarang menjadi tepat 00:00:00. ``` -## Autocorrection +## Koreksi otomatis -The *autocorrection* is a very handy feature of `Date` objects. We can set out-of-range values, and it will auto-adjust itself. +*Koreksi otomatis* adalah fitur yang sangat berguna dari objek `Date`. Kita bisa memasang nilai di luar batas, dan akan terkoreksi secara otomatis. -For instance: +Sebagai contoh: ```js run let date = new Date(2013, 0, *!*32*/!*); // 32 Jan 2013 ?!? -alert(date); // ...is 1st Feb 2013! +alert(date); // ...adalah 1st Feb 2013! ``` -Out-of-range date components are distributed automatically. +Komponen tanggal yang lewat dari batas akan terdistribusi secara otomatis. -Let's say we need to increase the date "28 Feb 2016" by 2 days. It may be "2 Mar" or "1 Mar" in case of a leap-year. We don't need to think about it. Just add 2 days. The `Date` object will do the rest: +Misalnya anda perlu menambahkan tanggal "28 Feb 2016" sebanyak 2 hari. Bisa saja menjadi "2 Mar" atau "1 Mar" apabila tahun kabisat. Kita tidak perlu memikirkannya. Tambahkan saja 2 hari. Objek `Date` akan mengerjakan sisanya: ```js run let date = new Date(2016, 1, 28); @@ -186,71 +188,72 @@ date.setDate(date.getDate() + 2); alert( date ); // 1 Mar 2016 ``` -That feature is often used to get the date after the given period of time. For instance, let's get the date for "70 seconds after now": +Fitur tersebut sering digunakan untuk mengambil nilai tanggal setelah periode waktu yang diberikan. Sebagai contoh, kita akan mengambil nilai tanggal "70 detik setelah sekarang": ```js run let date = new Date(); date.setSeconds(date.getSeconds() + 70); -alert( date ); // shows the correct date +alert( date ); // menunjukkan tanggal yang tepat ``` -We can also set zero or even negative values. For example: +Kita juga dapat memasang 0 atau bahkan nilai negatif. Sebagai contoh: ```js run let date = new Date(2016, 0, 2); // 2 Jan 2016 -date.setDate(1); // set day 1 of month +date.setDate(1); // memasang hari 1 dalam satu bulan alert( date ); -date.setDate(0); // min day is 1, so the last day of the previous month is assumed -alert( date ); // 31 Dec 2015 +date.setDate(0); // hari minimal adalah 1, jadi diasumsikan sebagai hari terakhir dari bulan sebelumnya +alert( date ); // 31 Des 2015 ``` ## Date to number, date diff +## Tanggal ke angka, perbedaan tanggal -When a `Date` object is converted to number, it becomes the timestamp same as `date.getTime()`: +Ketika sebuah objek `Date` dikonversi ke angka, akan menjadi timestamp yang sama dengan `date.getTime()`: ```js run let date = new Date(); -alert(+date); // the number of milliseconds, same as date.getTime() +alert(+date); // Angka dalam milidetik sama seperti date.getTime() ``` -The important side effect: dates can be subtracted, the result is their difference in ms. +Efek samping yang penting: tanggal bisa dikurangi, dan hasilnya adalah perbedaan dalam ms. -That can be used for time measurements: +Ini bisa digunakan untung perhitungan waktu: ```js run -let start = new Date(); // start measuring time +let start = new Date(); // mulai perhitungan waktu -// do the job +// melakukan perhitungan for (let i = 0; i < 100000; i++) { let doSomething = i * i * i; } -let end = new Date(); // end measuring time +let end = new Date(); // mengakhiri perhitungan waktu -alert( `The loop took ${end - start} ms` ); +alert( `Pengulangan memakan waktu ${end - start} ms` ); ``` ## Date.now() -If we only want to measure time, we don't need the `Date` object. +Jika kita ingin menghitung waktu, kita tidak memerlukan objek `Date`. -There's a special method `Date.now()` that returns the current timestamp. +Ada metode spesial `Date.now()` yang mengembalikan timestamp yang berlangsung. -It is semantically equivalent to `new Date().getTime()`, but it doesn't create an intermediate `Date` object. So it's faster and doesn't put pressure on garbage collection. +Secara semantik sama seperti `new Date().getTime()`, tapi tidak membuat objek `Date` perantara. Sehingga lebih cepat dan tidak menambahkan beban pada garbage collection. -It is used mostly for convenience or when performance matters, like in games in JavaScript or other specialized applications. +Ini sering digunakan untuk kenyamanan atau ketika performa menjadi penting, seperti dalam games dalam JavaScript atau aplikasi khusus lainnya. -So this is probably better: +Sehingga ini mungkin lebih baik: ```js run *!* -let start = Date.now(); // milliseconds count from 1 Jan 1970 +let start = Date.now(); // milidetik dihitung sejak 1 Jan 1970 */!* -// do the job +// melakukan perhitungan for (let i = 0; i < 100000; i++) { let doSomething = i * i * i; } @@ -259,19 +262,20 @@ for (let i = 0; i < 100000; i++) { let end = Date.now(); // done */!* -alert( `The loop took ${end - start} ms` ); // subtract numbers, not dates +alert( `Pengulangan memakan waktu ${end - start} ms` ); // mengurangi angka, bukan tanggal ``` ## Benchmarking -If we want a reliable benchmark of CPU-hungry function, we should be careful. +Jika kita memerlukan benchmark yang dapat diandalkan untuk fungsi yang CPU-hungry, kita harus berhati-hati. -For instance, let's measure two functions that calculate the difference between two dates: which one is faster? +Sebagai contoh, ukur dua fungsi yang menghitung perbedaan antara dua tanggal: mana yang lebih cepat? Such performance measurements are often called "benchmarks". +Perhitungan performa seperti ini biasa disebut "benchmarks". ```js -// we have date1 and date2, which function faster returns their difference in ms? +// kita punya date1 dan date2, fungsi mana yang mengembalikan perbedaan waktu dalam ms yang lebih cepat? function diffSubtract(date1, date2) { return date2 - date1; } @@ -282,13 +286,13 @@ function diffGetTime(date1, date2) { } ``` -These two do exactly the same thing, but one of them uses an explicit `date.getTime()` to get the date in ms, and the other one relies on a date-to-number transform. Their result is always the same. +Dua fungsi ini melakukan hal yang sama persis, namun satu fungsi menggunakan `date.getTime()` secara eksplisit untuk mendapatkan tanggal dalam ms, dan yang lain mengandalkan transformasi tanggal ke angka. Hasilnya hampir selalu sama. -So, which one is faster? +Jadi, mana yang lebih cepat? -The first idea may be to run them many times in a row and measure the time difference. For our case, functions are very simple, so we have to do it at least 100000 times. +Ide pertama yang mungkin adalah menjalankan fungsi tersebut berturut-turut dan hitung perbedaan waktunya. Dalam kasus ini, fungsi-fungsinya sangat sederhana, sehingga kita harus melakukannya minimal 100000 pengulangan. -Let's measure: +Lakukan perhitungan: ```js run function diffSubtract(date1, date2) { @@ -308,23 +312,23 @@ function bench(f) { return Date.now() - start; } -alert( 'Time of diffSubtract: ' + bench(diffSubtract) + 'ms' ); -alert( 'Time of diffGetTime: ' + bench(diffGetTime) + 'ms' ); +alert( 'Waktu dari diffSubtract: ' + bench(diffSubtract) + 'ms' ); +alert( 'Waktu dari diffGetTime: ' + bench(diffGetTime) + 'ms' ); ``` -Wow! Using `getTime()` is so much faster! That's because there's no type conversion, it is much easier for engines to optimize. +Wow! Menggunakan `getTime()` lebih cepat! Ini dikarenakan tidak ada konversi tipe, sehingga mesin lebih mudah untuk mengoptimalkannya. -Okay, we have something. But that's not a good benchmark yet. +Okay, kita sudah punya sesuatu. Namun itu belum dianggap benchmark yang bagus. -Imagine that at the time of running `bench(diffSubtract)` CPU was doing something in parallel, and it was taking resources. And by the time of running `bench(diffGetTime)` that work has finished. +Bayangkan saat menjalankan `bench(diffSubtract)` CPU sedang menjalankan sesuatu secara parallel, dan memakan sumber daya. Dan pada saat menjalankan `bench(diffGetTime)` pekerjaan tersebut sudah selesai. -A pretty real scenario for a modern multi-process OS. +Skenario yang nyata pada multi-process OS yang modern. -As a result, the first benchmark will have less CPU resources than the second. That may lead to wrong results. +Sebagai hasilnya, benchmark pertama akan menggunakan sumber daya CPU yang lebih sedikit daripada yang kedua. Ini akan berakibat kepada hasil yang salah. -**For more reliable benchmarking, the whole pack of benchmarks should be rerun multiple times.** +*Untuk benchmarking yand dapat diandalkan, semua percobaan benchmark harus dilakukan berulang kali.* -For example, like this: +Sebagai contoh, seperti ini: ```js run function diffSubtract(date1, date2) { @@ -348,53 +352,54 @@ let time1 = 0; let time2 = 0; *!* -// run bench(upperSlice) and bench(upperLoop) each 10 times alternating +// jalankan bench(upperSlice) dan bench(upperLoop) masing-masing 10 kali bergantian for (let i = 0; i < 10; i++) { time1 += bench(diffSubtract); time2 += bench(diffGetTime); } */!* -alert( 'Total time for diffSubtract: ' + time1 ); -alert( 'Total time for diffGetTime: ' + time2 ); +alert( 'Total waktu untuk diffSubtract: ' + time1 ); +alert( 'Total waktu untuk diffGetTime: ' + time2 ); ``` -Modern JavaScript engines start applying advanced optimizations only to "hot code" that executes many times (no need to optimize rarely executed things). So, in the example above, first executions are not well-optimized. We may want to add a heat-up run: +Mesin JavaScript modern mulai mengaplikasikan optimisasi lebih canggih hanya untuk "hot code" yang tereksekusi berulang kali (tidak perlu untuk mengoptimasi sesuatu yang jarang dilakukan). Sehingga, untuk contoh di atas, eksekusi pertama tidak terlalu dioptimasi. Kita mungkin perlu menambahkan percobaan pemanasan: ```js -// added for "heating up" prior to the main loop +// Tambahkan untuk "pemanasan" sebelum menjalankan pengulangan utama bench(diffSubtract); bench(diffGetTime); -// now benchmark +// lakukan benchmark for (let i = 0; i < 10; i++) { time1 += bench(diffSubtract); time2 += bench(diffGetTime); } ``` -```warn header="Be careful doing microbenchmarking" +```warn header="Berhati-hati ketika melakukan microbenchmarking" Modern JavaScript engines perform many optimizations. They may tweak results of "artificial tests" compared to "normal usage", especially when we benchmark something very small, such as how an operator works, or a built-in function. So if you seriously want to understand performance, then please study how the JavaScript engine works. And then you probably won't need microbenchmarks at all. +Mesin JavaScript modern melakukan banyak optimasi. Sehingga memungkinkan akan mengubah hasil dari "test buatan" dibandingkan "pemakaian normal", khususnya ketika kita melakukan benchmark pada sesuatu yang sangat kecil, seperti bagaimana operator bekerja, atau fungsi bawaan. Sehingga apabila anda serius ingin memahami performa, maka tolong pelajari bagaimana mesin JavaScript bekerja. Dan kemudian anda mungkin tidak memerlukan microbenchmark sama sekali. -The great pack of articles about V8 can be found at . +Sejumlah artikel bagus mengenai V8 dapat ditemukan di . ``` -## Date.parse from a string +## Date.parse dari sebuah string -The method [Date.parse(str)](mdn:js/Date/parse) can read a date from a string. +Metode [Date.parse(str)](mdn:js/Date/parse) dapat membaca tanggal dari sebuah string. -The string format should be: `YYYY-MM-DDTHH:mm:ss.sssZ`, where: +Format string harus seperti: `YYYY-MM-DDTHH:mm:ss.sssZ`, dimana: -- `YYYY-MM-DD` -- is the date: year-month-day. -- The character `"T"` is used as the delimiter. -- `HH:mm:ss.sss` -- is the time: hours, minutes, seconds and milliseconds. -- The optional `'Z'` part denotes the time zone in the format `+-hh:mm`. A single letter `Z` that would mean UTC+0. +- `YYYY-MM-DD` -- adalah tanggal: tahun-bulan-hari. +- Karakter `"T"` digunakan sebagai pembatas. +- `HH:mm:ss.sss` -- adalah waktu: jam, menit, detik, dan milidetik. +- Karakter `'Z'` adalah opsional yang menandakan zona waktu dalam format `+-hh:mm`. Satu karakter `Z` mempunyai arti UTC+0. -Shorter variants are also possible, like `YYYY-MM-DD` or `YYYY-MM` or even `YYYY`. +varian pendek juga bisa digunakan, seperti `YYYY-MM-DD` atau `YYYY-MM` atau bahkan `YYYY`. -The call to `Date.parse(str)` parses the string in the given format and returns the timestamp (number of milliseconds from 1 Jan 1970 UTC+0). If the format is invalid, returns `NaN`. +Pemanggilan `Date.parse(str)` akan mengubah string pada format yang diberikan dan mengembalikan timestamp (angka dalam milidetik dimulai sejak 1970 UTC+0). Apabila format tidak valid, akan mengembalikan `NaN`. -For instance: +Sebagai contoh: ```js run let ms = Date.parse('2012-01-26T13:51:50.417-07:00'); @@ -402,7 +407,7 @@ let ms = Date.parse('2012-01-26T13:51:50.417-07:00'); alert(ms); // 1327611110417 (timestamp) ``` -We can instantly create a `new Date` object from the timestamp: +Kita dapat membuat sebuah objek `new Date` secara instan dari timestamp: ```js run let date = new Date( Date.parse('2012-01-26T13:51:50.417-07:00') ); @@ -410,24 +415,25 @@ let date = new Date( Date.parse('2012-01-26T13:51:50.417-07:00') ); alert(date); ``` -## Summary +## Ringkasan -- Date and time in JavaScript are represented with the [Date](mdn:js/Date) object. We can't create "only date" or "only time": `Date` objects always carry both. -- Months are counted from zero (yes, January is a zero month). -- Days of week in `getDay()` are also counted from zero (that's Sunday). -- `Date` auto-corrects itself when out-of-range components are set. Good for adding/subtracting days/months/hours. -- Dates can be subtracted, giving their difference in milliseconds. That's because a `Date` becomes the timestamp when converted to a number. -- Use `Date.now()` to get the current timestamp fast. +- Tanggal dan waktu di JavaScript diwakili oleh objek [Date](mdn:js/Date). Kita tidak dapat membuat "hanya tanggal" atau "hanya waktu": Objek `Date` selalu membuat keduanya. +- Bulan dihitung mulai dari 0 (ya, Januari adalah bulan ke-0). +- Hari dalam satu minggu pada `getDay()` juga dihitung dari 0 (yaitu Minggu). +- `Date` terkoreksi otomatis ketika diberikan komponen di luar batas. Berguna untuk penambahan/pengurangan hari/bulan/jam. +- Tanggal dapat dikurangi, dan mendapatkan perbedaannya dalam milidetik. Ini dikarenakan sebuah `Date` menjadi timestamp ketika dikonversi ke angka. +- Gunakan `Date.now()` untuk mendapatkan timestamp yang sedang berlangsung dengan cepat. -Note that unlike many other systems, timestamps in JavaScript are in milliseconds, not in seconds. +Mohon diingat bahwa tidak sama dengan sistem lainnya, timestamp pada JavaScript adalah dalam milidetik, bukan detik. + +Terkadang kita perlu perhitungan waktu yang lebih presisi. JavaScript sendiri tidak mempunyai cara untuk mengitung waktu dalam mikrodetik (1 per sejuta dari 1 detik), tapi environment pada umumnya menyediakannya. Sebagai contoh, browser punya [performance.now()](mdn:api/Performance/now) yang mengembalikan angka dalam milidetik dimulai dari awal halaman terpasang dengan presisi mikrodetik (3 digit setelah koma): -Sometimes we need more precise time measurements. JavaScript itself does not have a way to measure time in microseconds (1 millionth of a second), but most environments provide it. For instance, browser has [performance.now()](mdn:api/Performance/now) that gives the number of milliseconds from the start of page loading with microsecond precision (3 digits after the point): ```js run alert(`Loading started ${performance.now()}ms ago`); -// Something like: "Loading started 34731.26000000001ms ago" -// .26 is microseconds (260 microseconds) -// more than 3 digits after the decimal point are precision errors, but only the first 3 are correct +// Seperti: "Loading started 34731.26000000001ms ago" +// .26 adalah mikrodetik (260 mikrodetik) +// lebih dari 3 digit setelah koma adalah error pada presisi, hanya 3 angka pertama yang benar ``` Node.js has `microtime` module and other ways. Technically, almost any device and environment allows to get more precision, it's just not in `Date`.