Skip to main content

DTG & Stardate

I've been using the DTG (Date Time Group) script I made years ago over and over for full screen clocks of various types as I often use a Raspberry Pi or old computer as a display/terminal in the house. I recently updated it to include showing the Stardate. If you search online you will find a million different Stardate types and opinions. My version continues on with the season/episode convention that was eventually used in The Next Generation series, which is why the then date was set for July 15, 1987. 

function updateTimeAndDate() {
    const now = new Date();
    // Calculate time zone difference
    const timeZoneDifference = now.getTimezoneOffset() / 60; // in hours
    // Update the HTML element with the id "time-zone-difference"
    const localTimeZoneName = Intl.DateTimeFormat().resolvedOptions().timeZone;
    const year = now.getFullYear();
    const month = String(now.getMonth() + 1).padStart(2, '0');
    const day = String(now.getDate()).padStart(2, '0');
    const hours = String(now.getHours()).padStart(2, '0');
    const minutes = String(now.getMinutes()).padStart(2, '0');
    const seconds = String(now.getSeconds()).padStart(2, '0');
    const formattedDate = `${year}-${month}-${day}`;
    const formattedTime = `${hours}:${minutes}:${seconds}`;
    document.getElementById("dtg-date").textContent = formattedDate;
    document.getElementById('dtg-clock').textContent = formattedTime;
    document.getElementById("dtg-zone").textContent = localTimeZoneName;
    // Calculate and display Stardate
    const then = new Date("July 15, 1987");
    const stardate = Math.floor((now.getTime() - then.getTime()) / (1000 * 60 * 60 * 24 * 0.036525) + 410000) / 10;
    document.getElementById("stardate").textContent = stardate;
  }
  updateTimeAndDate();