How to Insert the Date and Time in a Website

Techwalla may earn compensation through affiliate links in this story. Learn more about our affiliate and product review process here.
Add the date and time to the code for your website.
Image Credit: Baitong333/iStock/Getty Images

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.

Advertisement

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.

Video of the Day

Step 2

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

Advertisement

This creates an empty "div" block.

Step 3

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

Advertisement

Advertisement

Advertisement

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.

Advertisement

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.

Video of the Day

Advertisement

Advertisement

references & resources

Report an Issue

screenshot of the current page

Screenshot loading...