How to Insert the Date and Time in a Website

By Kevin Lee

Adding the current date and time to a website ensures that at least one thing on the site changes when a site visitor refreshes a page. You don't need 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.

Step 1

Open one of your Web pages using Notepad, an HTML editor or any application that produces plain text. To assure that you retain a copy of your unedited site code, duplicate the file before you edit it.

Step 2

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

This creates an empty "div" block.

Step 3

Add the following code before the closing "" tag in your HTML:This JavaScript function runs after the page loads. The "dateTime" variable holds the system date and time. The "formattedDateTime" variable breaks dateTime into two formatted values, one for the date and one for the time, 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.

Step 4

Save the document and open it in your browser. The date and time appear in the location in which you placed the "div" tag. Reload the page to see the values change.

Tips & Warnings

  • The separator variable can contain any character you select. This example uses a dash.