How to Insert the Date and Time in a Website

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.

html code
Add the date and time to the code for your website.
credit: Baitong333/iStock/Getty Images

Video of the Day


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.


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

This creates an empty "div" block.


Add the following code before the closing "" tag in your HTML:

var divObject = document.getElementById("dateTimeValue"); divObject.innerHTML = formattedDateTime; }

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.


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.

Show Comments