Jump to content

tom-martin

Circle Member
  • Posts

    15
  • Joined

  • Last visited

Reputation Activity

  1. Like
    tom-martin reacted to paul2009 in Time-based headings   
    You can achieve this with some simple JavaScript. Here's an example that you can paste into a Code Block on your page and then adjust to match your requirements.
    <h2 id="greetingEl"><h2> <script> const time = new Date().getHours(); let greeting; if (time < 12) {   greeting = "Good morning"; } else if (time < 19) {   greeting = "Good afternoon"; } else {   greeting = "Good evening"; } document.getElementById("greetingEl").innerHTML = greeting; </script> The first section creates a heading element where the greeting will appear. 
    Below this is the script, which gets the time and then extracts the hour from this using the getHours() method. This returns an hour from 0 to 23 and this is used to determine which greeting to show. For a deeper explanation of this, see JavaScript if else and else if.
      If this post has helped you, please click a 'Like' or 'Thanks' icon below  ⬇️
  2. Like
    tom-martin got a reaction from tuanphan in Time-based headings   
    Amazing, thank you so much. Worked perfectly! 
×
×
  • Create New...

Squarespace Webinars

Free online sessions where you’ll learn the basics and refine your Squarespace skills.

Hire a Designer

Stand out online with the help of an experienced designer or developer.