Build a Live Time Display in Webflow: JavaScript Tutorial

Published on
October 21, 2019

Building a Live Time Display with Webflow

Webflow is a powerful tool for creating websites and web applications. One of its key features is the ability to incorporate dynamic elements such as live time displays using JavaScript. In this tutorial, we will learn how to display the current time on a webpage and format it in various ways using Webflow and JavaScript.

Getting Started with Webflow Live Time Display

To begin, open your Webflow project and navigate to the page where you want to display the live time. In this example, we will create a simple live time display that shows both the 12-hour and 24-hour versions of the current time.

Adding Text Elements for Time Display

First, add a text element to your page where you want the time to be displayed. You can use a heading, paragraph, or text element with a unique class that we will target in JavaScript.

Applying Unique Classes

Apply unique classes to the text elements that you added for displaying the time. This will allow us to target these elements in JavaScript and update their content with the current time.

Implementing JavaScript for Live Time Display

Now, let's implement the JavaScript code that will display the live time on the webpage. We will create two different versions of the time display, one for the 12-hour format and another for the 24-hour format.

Adding JavaScript Code to the Page

  1. Move to the end of the body tag in your Webflow project.
  2. Insert a script tag to add the JavaScript code for the live time display.
<script>  // JavaScript code for live time display goes here</script>

Creating Time Formatting Options

In the script tag, create two variables to hold the formatting options for the 12-hour and 24-hour time formats. We will customize these options to control how the time is displayed.

<script>  // Create variables for time formatting options  var timeVersion12 = {    timeZone: 'America/New_York',    hour12: true,    hour: '2-digit',    minute: '2-digit',    second: '2-digit'  };    var timeVersion24 = {    timeZone: 'America/New_York',    hour12: false,    hour: '2-digit',    minute: '2-digit',    second: '2-digit'  };</script>

Displaying Time on the Webpage

Now, we will use JavaScript to update the text content of the elements with the current time based on the formatting options we defined.

<script>  // Update time display for 12-hour format  document.querySelector('.hack-23-time-version-1').textContent = new Date().toLocaleTimeString('en-US', timeVersion12);  // Update time display for 24-hour format  document.querySelector('.hack-23-time-version-2').textContent = new Date().toLocaleTimeString('en-US', timeVersion24);</script>

Customizing the Time Display

You can customize the time formatting options by modifying the variables timeVersion12 and timeVersion24. This allows you to change the time zone, enable or disable the 12-hour format, and control the display of hours, minutes, and seconds.

Modifying Time Formatting Options

To change the time formatting options, update the properties of the timeVersion12 and timeVersion24 variables. You can refer to the Mozilla Developer Docs for a full overview of available options and their usage.

Using the Mozilla Developer Docs

The Mozilla Developer Docs provide comprehensive information on the toLocaleTimeString() method and its options. You can explore various time zone settings, time stylings, and hour cycle options to customize the time display according to your preferences.

Testing the Live Time Display

After adding the JavaScript code and customizing the time formatting options, preview the webpage in the Webflow Designer or publish it to see the live time display in action. When you load the page or reload it, you will see the current time displayed in the formats you specified.

Conclusion

In this tutorial, we explored how to build a live time display in Webflow using JavaScript. By adding unique classes to text elements and applying custom JavaScript code, we were able to display the current time on a webpage and format it in different ways. With the flexibility and power of Webflow, you can create dynamic and interactive content for your websites and web applications.

Start experimenting with live time displays and explore the possibilities of incorporating dynamic content into your Webflow projects. Try out different time formatting options and enhance the user experience with real-time updates. Thank you for learning about building a live time display with Webflow!