How to insert the date and time in a website

Updated April 17, 2017

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.

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

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:

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.

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


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

Cite this Article A tool to create a citation to reference this article Cite this Article

About the Author

After majoring in physics, Kevin Lee began writing professionally in 1989 when, as a software developer, he also created technical articles for the Johnson Space Center. Today this urban Texas cowboy continues to crank out high-quality software as well as non-technical articles covering a multitude of diverse topics ranging from gaming to current affairs.