From 3a640eb7d3065ca58eec125a0aaacb8e69193335 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexis=20S=C3=A1nchez?= Date: Mon, 20 May 2024 18:32:53 +0200 Subject: [PATCH] refactor(index.html): improve updateDate function for better readability and performance by optimizing date calculations and updating element text content directly --- index.html | 60 +++++++++++++++++++++++++++++++----------------------- 1 file changed, 34 insertions(+), 26 deletions(-) diff --git a/index.html b/index.html index 569e3a0..85fb1b9 100644 --- a/index.html +++ b/index.html @@ -58,33 +58,41 @@

Remaining Time until chunk.padStart(8, '0')); +function updateDate() { + const currentDate = new Date(); + const utcDate = currentDate.toISOString().slice(0, 19).replace('T', ' '); + const epochTime = Math.floor(currentDate.getTime() / 1000); + const binaryString = "0" + epochTime.toString(2); + const binaryChunks = binaryString.match(/.{1,8}/g); + + // Assuming the binary chunks are already 8 bits, no need to pad them again + const paddedBinaryChunks = binaryChunks.map(chunk => chunk.padStart(8, '0')); + + document.getElementById("current-date").textContent = "Date: " + utcDate; + document.getElementById("epoch-time").textContent = "Epoch Time: " + epochTime; + document.getElementById("epoch-binary").textContent = "Binary: " + paddedBinaryChunks.join(" "); + + const targetDate = new Date("2038-01-19T03:14:07Z").getTime() / 1000; + const remainingTime = targetDate - epochTime; + + const secondsInMinute = 60; + const secondsInHour = secondsInMinute * 60; + const secondsInDay = secondsInHour * 24; + const secondsInYear = secondsInDay * 365.25; // Account for leap years + const secondsInMonth = secondsInYear / 12; + + const years = Math.floor(remainingTime / secondsInYear); + const months = Math.floor((remainingTime % secondsInYear) / secondsInMonth); + const days = Math.floor((remainingTime % secondsInMonth) / secondsInDay); + const hours = Math.floor((remainingTime % secondsInDay) / secondsInHour); + const minutes = Math.floor((remainingTime % secondsInHour) / secondsInMinute); + const seconds = Math.floor(remainingTime % secondsInMinute); + + const remainingTimeString = `${years} years, ${months} months, ${days} days, ${hours} hours, ${minutes} minutes, ${seconds} seconds`; + document.getElementById("remaining-time").textContent = remainingTimeString; +} - document.getElementById("current-date").textContent = "Date: " + utcDate; - document.getElementById("epoch-time").textContent = "Epoch Time: " + epochTime; - document.getElementById("epoch-binary").textContent = "Binary: " + paddedBinaryChunks.join(" "); - - const targetDate = new Date("2038-01-19T03:14:07Z").getTime() / 1000; - const remainingTime = targetDate - epochTime; - - const years = Math.floor(remainingTime / (365 * 24 * 60 * 60)); - const months = Math.floor((remainingTime % (365 * 24 * 60 * 60)) / (30 * 24 * 60 * 60)); - const days = Math.floor((remainingTime % (30 * 24 * 60 * 60)) / (24 * 60 * 60)); - const hours = Math.floor((remainingTime % (24 * 60 * 60)) / (60 * 60)); - const minutes = Math.floor((remainingTime % (60 * 60)) / 60); - const seconds = Math.floor(remainingTime % 60); - - const remainingTimeString = `${years} years, ${months} months, ${days} days, ${hours} hours, ${minutes} minutes, ${seconds} seconds`; - document.getElementById("remaining-time").textContent = remainingTimeString; - } - - setInterval(updateDate, 10); + setInterval(updateDate, 100);