Website as Clock: how to make a time-specific website

(i) use ← and → keys to go through slides

What does this number mean? It’s a Unix Timestamp: the number of seconds since the Unix Epoch on January 1st 1970 at UTC (Coordinated Universal Time). It’s how computers can count and tell the time. It’s universal and not timezone-dependent.

It was when you loaded this webpage.

How can a website tell me the time? (in a way I can understand) The website will ask your device for your local time, and through Javascript can display it using the new Date() function. A Date is a moment captured once. It does not update by itself.

It is right now.

How to make the website work like a clock? To get a live clock, you have to continuously ask the time, using setInterval() which calls a function at specified intervals (in milliseconds).
function tellTime() {
    var now = new Date();
    document.getElementById("now").innerHTML = now;
}

document.addEventListener("DOMContentLoaded", () => {
    // Start the clock upon loading the webpage
    tellTime();
    // Update the clock every second (1000 milliseconds)
    setInterval(tellTime, 1000);
});
Make sure to place an element with the "now" id inside the body of your html:
<p>It is <span id="now"></span> right now.</p>

::

How to display the time/date in different ways? We can display and thereby style the clock digits or date separately, or show the day of the week by updating our clock script as follows:
function tellTime() {
    var now = new Date();
    document.querySelector("#now").innerHTML = now;

    // Get time separately
    var time = now.toTimeString();
    document.querySelector("#time").innerHTML = time;

    // Get date separately
    var date = now.toDateString();
    document.querySelector("#date").innerHTML = date;

    // Get hours/minutes/seconds separately
    var hours = now.getHours();
    var minutes = now.getMinutes();
    var seconds = now.getSeconds();
    // Get the hour
    // (hours < 10 ? '0' : "") -> makes sure there is a 0 before the number if it’s a single digit (0-9)
    document.querySelector("#hours").innerText = (hours < 10 ? '0' : "") + hours;
    // Same for minutes and seconds
    document.querySelector("#minutes").innerText = (minutes < 10 ? '0' : "") + minutes;
    document.querySelector("#seconds").innerText = (seconds < 10 ? '0' : "") + seconds;

    // Get the day of the week
    // getDay() will give a number from 0 to 6, 0 being Sunday, 1 Monday etc. We first need to list this though:
    var weekday = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];
    // Now we can ask for the number and translate it to the corresponding item in our list of days.
    var day = weekday[now.getDay()];
    document.querySelector("#weekday").innerHTML = day;
}
Make sure to place the following elements inside the body of your html:
<p>
    <span id="time"></span> 
</p>
<p>
    <span id="date"></span> 
</p>
<p>
    <span id="day"></span> <span id="month"></span> <span id="year"></span> 
</p>
<div class="clock">
    <span id="hours"></span>:<span id="minutes"></span>:<span id="seconds"></span>
</div>
<p>
    <span id="weekday"></span> 
</p>
More options can be found here.

How can time transform the website? Through if statements inside your clock script, you can create changes in the styling or content of your website. The script checks whether the current time is before (<) a given time and applies the corresponding changes. An easy way to apply many conditional style changes through CSS rather than Javascript is to let the script add a class to the body (e.g. .darkmode) and style selected elements inside that class.
function tellTime() {
    var now = new Date();

    // Get time separately
    var time = now.toTimeString();
    document.querySelector("#time").innerHTML = time;

    // Time-dependent styling/content
    var message = document.getElementById("message");
    // Before 06:00...
    if (time < "06:00:00") { 
        message.innerText = "Zzzzz";
        document.body.classList.add('darkmode');
    }
    // After 06:00 and before 12:00...
    else if (time < "12:00:00") { 
        message.innerText = "Good morning!";
    }
    else if (time < "18:00:00") { 
        message.innerText = "Good afternoon!";
        message.style.color = "orange";
    }
    else if (time < "21:00:00") { 
        message.innerText = "Good evening!";
        message.style.color = "inherit";
    }
    // After 21:00
    else {
        message.innerText = "Good night!";
        document.body.classList.add('darkmode');
    }
}
Make sure to place the following elements inside the body of your html:
<p id="time">...</p>
<p id="message">...</p>

How can time + weekday transform the website? You can make the time schedule specific to a day by nesting if statements.
function tellTime() {
    var now = new Date();

    // Get time separately
    var time = now.toTimeString();

    // Get the day of the week
    var weekday = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];
    var day = weekday[now.getDay()];

    // Weekday-dependent styling/content
    var mondayMessage = document.getElementById("monday-message");
    if (day == "Monday") {
        if (time < "09:15:00") { 
            mondayMessage.innerText = "No class yet...";
        }
        else if (time < "12:30:00") { 
            mondayMessage.innerText = "Hello 2A!";
        }
        else if (time < "13:15:00") { 
            mondayMessage.innerText = "Lunch time!";
        }
        else if (time < "16:30:00") { 
            mondayMessage.innerText = "Hello 2B!";
        }
        else {
            mondayMessage.innerText = "See you next Monday!";
        }
    }
    else {
        mondayMessage.innerText = "No class today.";
    }
}
Make sure to place the following element inside the body of your html:
<p id="monday-message">...</p>

...

(till 2038-01-19 00:00 UTC)


On this date the Unix Time Stamp will cease to work due to a 32-bit overflow. Before this moment millions of applications will need to either adopt a new convention for time stamps or be migrated to 64-bit systems which will buy the time stamp a "bit" more time. (source)


How to make a countdown timer with progress-bar? See the script for this example with the Unix Epoche as startdate and the Year 2038 (32-bit overflow) problem as the enddate
// COUNTDOWN TIMER
// The date we're counting from and the date we're counting down to
var startDate = new Date("Jan 1, 1970 00:00:00 UTC").getTime();
var countDownDate = new Date("Jan 19, 2038 00:00:00 UTC").getTime();
var totalDuration = countDownDate - startDate;

function updateCountdown() {

  // Get today's date and time
  var now = new Date().getTime();

  // Find the distance between now and the count down date
  var distance = countDownDate - now;

  // Time calculations for days, hours, minutes and seconds
  var daysLeft = Math.floor(distance / (1000 * 60 * 60 * 24));
  var hoursLeft = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
  var minutesLeft = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
  var secondsLeft = Math.floor((distance % (1000 * 60)) / 1000);

  // Progress: share of the total time that has passed (clamped to 0-100)
  var progress = ((now - startDate) / totalDuration) * 100;
  progress = Math.min(100, Math.max(0, progress));

  document.getElementById("progress-bar").style.width = progress + "%";

  // If the count down is finished, write some text
  if (distance < 0) {
    clearInterval(countdown);
    document.getElementById("countdown-timer").innerHTML = "EXPIRED";
    document.getElementById("progress-bar").style.width = "100%";
    return;
  }

  // Display the result in the element with id="countdown-timer"
  document.getElementById("countdown-timer").innerHTML = daysLeft + "d " + hoursLeft + "h "
    + minutesLeft + "m " + secondsLeft + "s ";
}

updateCountdown(); // run once immediately so there's no 1s blank
var countdown = setInterval(updateCountdown, 1000);

See here the css for the progress bar:

#progress-container {
    width: 100%;
    max-width: 800px;
    height: 30px;
    margin: 20px auto;
    background-color: gainsboro;
    overflow: hidden;
}

#progress-bar {
    width: 0%;
    height: 100%;
    background-color: red;
    transition: width 1s linear;
}
Make sure to place the following elements inside the body of your html:
<p id="countdown-timer">...</p>
<div id="progress-container">
    <div id="progress-bar"></div>
</div> 

How to make a progress-bar update according to a schedule? You can update the start and enddate within the if statements of the schedule with setHours(h, m, s). See the code below or check the example html here.
function timeSchedule() {
    var now = new Date();

    // Get time separately
    var time = now.toTimeString();

    // Start and end of the current time segment (set in each branch below)
    var startDate = new Date(now);
    var endDate = new Date(now);

    // Time-dependent styling/content
    var message = document.getElementById("message");
    if (time < "09:15:00") {
        message.innerText = "Class hasn’t started yet.";
        startDate.setHours(0, 0, 0);
        endDate.setHours(9, 15, 0);
    }
    else if (time < "12:30:00") {
        message.innerText = "Morning class is in progress!";
        startDate.setHours(9, 15, 0);
        endDate.setHours(12, 30, 0);
    }
    else if (time < "13:15:00") {
        message.innerText = "Lunch break!";
        startDate.setHours(12, 30, 0);
        endDate.setHours(13, 15, 0);
    }
    else if (time < "16:30:00") {
        message.innerText = "Afternoon class is in progress!";
        startDate.setHours(13, 15, 0);
        endDate.setHours(16, 30, 0);
    }
    else {
        message.innerText = "Have a nice rest of the day!";
        startDate.setHours(16, 30, 0);
        endDate.setHours(24, 0, 0); // 24 rolls over to midnight
    }

    // Same calculations as in the countdown, now using this segment's start and end
    var totalDuration = endDate - startDate;
    var distance = endDate - now;

    var hoursLeft = Math.floor(distance / (1000 * 60 * 60));
    var minutesLeft = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
    var secondsLeft = Math.floor((distance % (1000 * 60)) / 1000);

    var progress = ((now - startDate) / totalDuration) * 100;
    progress = Math.min(100, Math.max(0, progress));

    document.getElementById("progress-bar").style.width = progress + "%";
    document.getElementById("countdown-timer").innerHTML =
        hoursLeft + "h " + minutesLeft + "m " + secondsLeft + "s left";
}

timeSchedule(); // run once immediately
setInterval(timeSchedule, 1000);

See here the css for the progress bar:

#progress-container {
    width: 100%;
    max-width: 800px;
    height: 30px;
    margin: 20px auto;
    background-color: gainsboro;
    overflow: hidden;
}

#progress-bar {
    width: 0%;
    height: 100%;
    background-color: red;
    transition: width 1s linear;
}
Make sure to place the following elements inside the body of your html:
<p id="message">...</p>
<div id="progress-container">
    <div id="progress-bar"></div>
</div>
<p id="countdown-timer">...</p> 

till January 19, 2038

How to make changes based on the countdown progress? You can base the if statements on the percentage of progress. See the code below or check the example html here.
// The date we're counting from and the date we're counting down to
var startDate = new Date("Jan 1, 1970 00:00:00 UTC").getTime();
var countDownDate = new Date("Jan 19, 2038 00:00:00 UTC").getTime();
var totalDuration = countDownDate - startDate;

function tellProgress() {
    var now = new Date().getTime();

    // Same calculations as in the countdown
    var distance = countDownDate - now;
    var daysLeft = Math.floor(distance / (1000 * 60 * 60 * 24));
    var hoursLeft = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
    var minutesLeft = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
    var secondsLeft = Math.floor((distance % (1000 * 60)) / 1000);

    // Progress: share of the total time that has passed (clamped to 0-100)
    var progress = ((now - startDate) / totalDuration) * 100;
    progress = Math.min(100, Math.max(0, progress));

    // Show the numbers and the bar
    document.getElementById("progress-bar").style.width = progress + "%";
    document.getElementById("countdown-timer").innerHTML =
        daysLeft + "d " + hoursLeft + "h " + minutesLeft + "m " + secondsLeft + "s";

    // Progress-dependent styling/content
    var message = document.getElementById("message");
    var bar = document.getElementById("progress-bar");
    // Before 25%...
    if (progress < 25) {
        message.innerText = "Plenty of time left";
        bar.style.backgroundColor = "green";
    }
    // After 25% and before 50%...
    else if (progress < 50) {
        message.innerText = "Getting closer";
        bar.style.backgroundColor = "yellowgreen";
    }
    else if (progress < 75) {
        message.innerText = "More than halfway!";
        bar.style.backgroundColor = "orange";
    }
    else if (progress < 100) {
        message.innerText = "Almost there! 😬";
        bar.style.backgroundColor = "red";
    }
    // At 100%
    else {
        message.innerText = "EXPIRED";
        clearInterval(countdown);
    }
}

tellProgress(); // run once immediately
var countdown = setInterval(tellProgress, 1000);

See here the css for the progress bar:

#progress-container {
    width: 100%;
    max-width: 800px;
    height: 30px;
    margin: 20px auto;
    background-color: gainsboro;
    overflow: hidden;
}

#progress-bar {
    width: 0%;
    height: 100%;
    background-color: red;
    transition: width 1s linear;
}
Make sure to place the following elements inside the body of your html:
<p><span id="countdown-timer"></span> till January 19, 2038</p>
<div class="progress-container">
    <div id="progress-bar"></div>
</div>
<p id="message"></p>

24855d 1h 0m 0s till January 19, 2038

Plenty of time left

How to time travel within your website? The simplest way to see a snapshot of your website at a given time is to insert that specific date/time into "var now = new Date()":
function tellProgress() {
    var now = new Date("1970-01-01T00:00:00").getTime();

    // The rest of your script below
    // ...
}

It’s currently in Hong Kong.

How to show the time of a specific location? You can use toLocaleTimeString() to return the time of a specific timezone (and use locale conventions). Find the country codes and tz (timezone) identifier here.
function tellLocalTime() {
    var now = new Date();
    
    var localTime = now.toLocaleTimeString("hk-HK", {timeZone: "Asia/Hong_Kong"});
    document.getElementById("local-time").innerHTML = localTime;

    // The rest of your script below (make sure to update "time" to "localTime" in your schedule if applicable!)
    if (localTime < "07:00:00") {
    // ...
    }
}