Pernahkah Anda mencoba menghitung jumlah hari antara dua tanggal di JavaScript tanpa library?
Jawaban singkat
const firstDate = new Date("05/10/2022") // 10th May, 2022 const secondDate = new Date() // today, 14th May, 2022 const millisecondsDiff = secondDate.getTime() - firstDate.getTime() const daysDiff = Math.round( millisecondsDiff / (24 * 60 * 60 * 60) )
Masuk ke mode layar penuh Keluar dari mode layar penuh
Jawaban panjang
Untuk mengetahui perbedaan antara dua tanggal, pertama-tama Anda harus mendapatkan nilai waktu dalam milidetik. Anda mendapatkan ini menggunakan metode getTime_ pada contoh Date
const firstDate = new Date("05/10/2022") // 10th May, 2022 const secondDate = new Date() // today, 14th May, 2022 const firstDateInMs = firstDate.getTime() const secondDateInMs = secondDate.getTime() _
Masuk ke mode layar penuh Keluar dari mode layar penuh
Nilai milidetik adalah angka di mana Anda kemudian dapat melakukan operasi aritmatika seperti pengurangan
Langkah selanjutnya adalah mencari selisih antara kedua tanggal tersebut
const differenceBtwDates = secondDateInMs - firstDateInMs console.log(differenceBtwDates) // 351628869
Masuk ke mode layar penuh Keluar dari mode layar penuh
Sekarang, Anda memiliki perbedaan milidetik antara kedua tanggal. Di sinilah Anda mengonversi milidetik menjadi hari. Bagaimana cara melakukannya?
Pertama, Anda mendapatkan kesetaraan 1 hari (24 jam) dalam milidetik. Begini caranya
const aDayInMs = 24 * 60 * 60 * 1000 console.log(aDayInMs) // 86400000
Masuk ke mode layar penuh Keluar dari mode layar penuh
- 24 berarti 24 jam
- 60 berarti 60 menit, yang berarti satu jam, sehingga mengubah 24 jam menjadi menit
- 60 berikutnya berarti 60 detik, yang berarti satu menit
- dan 1000 berarti 1000 milidetik, yang berarti satu detik
Sekarang setelah Anda memiliki nilai milidetik untuk satu hari, Anda dapat menggunakannya untuk menentukan berapa hari perbedaan antara tanggal tersebut
Jika 86400000 (const firstDate = new Date("05/10/2022") // 10th May, 2022
const secondDate = new Date() // today, 14th May, 2022
const firstDateInMs = firstDate.getTime()
const secondDateInMs = secondDate.getTime()
0) milidetik menghasilkan satu hari, maka, const firstDate = new Date("05/10/2022") // 10th May, 2022
const secondDate = new Date() // today, 14th May, 2022
const firstDateInMs = firstDate.getTime()
const secondDateInMs = secondDate.getTime()
1 (const firstDate = new Date("05/10/2022") // 10th May, 2022
const secondDate = new Date() // today, 14th May, 2022
const firstDateInMs = firstDate.getTime()
const secondDateInMs = secondDate.getTime()
2) milidetik menghasilkan
const daysDiff = differenceBtwDates / aDayInMs console.log(daysDiff) // 4.0782153125
Masuk ke mode layar penuh Keluar dari mode layar penuh
Karena mengembalikan angka mengambang, Anda dapat membulatkannya dengan const firstDate = new Date("05/10/2022") // 10th May, 2022
const secondDate = new Date() // today, 14th May, 2022
const firstDateInMs = firstDate.getTime()
const secondDateInMs = secondDate.getTime()
3
const daysDiff = Math.round(differenceBtwDates / aDayInMs) console.log(daysDiff) // 4 _
Masuk ke mode layar penuh Keluar dari mode layar penuh
Membandingkan 10 Mei dan 14 Mei, perbedaannya adalah 4 hari
Ini kode lengkapnya
const firstDate = new Date("05/10/2022") // 10th May, 2022 const secondDate = new Date() // today, 14th May, 2022 const firstDateInMs = firstDate.getTime() const secondDateInMs = secondDate.getTime() const differenceBtwDates = secondDateInMs - firstDateInMs const aDayInMs = 24 * 60 * 60 * 1000 const daysDiff = Math.round(differenceBtwDates / aDayInMs) console.log(daysDiff) // 4
Masuk ke mode layar penuh Keluar dari mode layar penuh
const firstDate = new Date("05/10/2022") // 10th May, 2022 const secondDate = new Date() // today, 14th May, 2022 const firstDateInMs = firstDate.getTime() const secondDateInMs = secondDate.getTime() _4 dan const firstDate = new Date("05/10/2022") // 10th May, 2022 const secondDate = new Date() // today, 14th May, 2022 const firstDateInMs = firstDate.getTime() const secondDateInMs = secondDate.getTime() 5 dapat berupa tanggal apa saja. Saya menggunakan 10 dan 14 Mei untuk tujuan penjelasan