How to insert the date and time in a website

Written by kevin lee
  • Share
  • Tweet
  • Share
  • Pin
  • Email
How to insert the date and time in a website
Since time is everywhere, add it to your website. (time image by timur1970 from Fotolia.com)

Adding the current date and time to a website ensures that at least one thing on the site changes each time a site visitor refreshes the page. You do not have to learn advanced programming languages such as PHP, JAVA or ASP.NET to work with date and time formats. Using JavaScript and basic HTML, you can insert the date and time in your website with a few lines of code.

Skill level:
Easy

Other People Are Reading

Instructions

  1. 1

    Open one of your web pages using Notepad or an HTML editor.

  2. 2

    Add the following code after the first "<body>" tag in your HTML:

    <div id="dateTimeValue"</div>

    This creates an empty "div" block.

  3. 3

    Add the following code before the closing "</head>" tag in your HTML:

    <script type="text/JavaScript">

    window.onload = function () {

    var separator = " - ";

    var date Time = new Date();

    var formattedDateTime = dateTime.toLocaleDateString() + separator + dateTime.toLocaleTimeString()

    var divObject = document.getElementById("dateTimeValue");

    divObject.innerHTML = formattedDateTime;

    }

    </script>

    This JavaScript function runs after the page loads. The "dateTime" variable holds the system date and time. The "formattedDateTime" variable breaks the date Time into a formatted date value and a formatted time value separated by the character defined in "separator." The last two statements add the formatted date and time string to the "div" tag defined in the body section of the HTML.

  4. 4

    Save the document and open it in your browser. The date and time will appear in the location where you placed the "div." tag.

Tips and warnings

  • Change the separator variable to any character that you like. In this example, the character is a dash (-).

Don't Miss

Filter:
  • All types
  • Articles
  • Slideshows
  • Videos
Sort:
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the eHow.co.uk site, you consent to the use of cookies. For more information, please see our Cookie policy.