Quick actions

cmd+k|ctrl+k

Navigation

Languages

Hitung durasi pertanggal, bulan, tahun, jam

Snippet info

Language

JavaScript

Visibility

public

Author

riki.whyudi

Created

2023-01-23T01:09:03.232634Z

Updated

2023-01-23T01:09:03.232634Z

let now = new Date();

// Tampilkan tanggal, bulan, dan tahun sekarang
console.log("Tanggal sekarang: " + now.getDate());
console.log("Bulan sekarang: " + (now.getMonth() + 1));
console.log("Tahun sekarang: " + now.getFullYear());
console.log("Waktu sekarang: " + now.toLocaleTimeString());

// tambahkan satu bulan ke waktu sekarang
let nextMonth = new Date(now.getFullYear(), now.getMonth() + 1, now.getDate(), now.getHours(), now.getMinutes(), now.getSeconds());

//Perhitungan jumlah hari pada bulan tersebut
let endMonth = nextMonth.getMonth();
if (endMonth === 1) { // Februari
  if (nextMonth.getFullYear() % 4 === 0) { // kabisat
    if (nextMonth.getDate() > 29) {
      nextMonth.setDate(29);
    }
  } else if (nextMonth.getDate() > 28) {
    nextMonth.setDate(28);
  }
} else if ([3, 5, 8, 10].includes(endMonth)) {
  if (nextMonth.getDate() > 30) {
    nextMonth.setDate(30);
  }
}

// Tampilkan tanggal, bulan, dan tahun satu bulan kemudian
console.log("Tanggal satu bulan kemudian: " + nextMonth.getDate());
console.log("Bulan satu bulan kemudian: " + (nextMonth.getMonth() + 1));
console.log("Tahun satu bulan kemudian: " + nextMonth.getFullYear());
console.log("Waktu satu bulan kemudian: " + nextMonth.toLocaleTimeString());
INFO