How to display the date and time of a document when it is last modified in JavaScript?


In this article we are going to discuss how to display the date and time of a document when it is last modified in JavaScript.

It is important to know the last updated date and time of a web document, when you read some content on the web, to know whether the web document is latest or outdated.

The Document object has lastModified property which returns us the last modified date and time of a document. This is a read-only property. The value of the lastModified property is obtained by the HTTP header from the web server.

For people who follow the latest technical articles, the lastModified property helps to know whether the article or web documents are latest or outdated.

Syntax

The syntax to display the last modified date and time of a document is shown below.

document.lastModified;

A string containing the date and time is returned.

Example 1

Following is the example where we displayed the last modified date and time of the document.

<html>
<body>
   <script>
      document.write("This document is lastly modified on" + " " +document.lastModified);
   </script>
</body>
</html>

On executing the above code, the following output is generated.

Let’s look at another example program to display the last modified date and time of the document.

Example

<!DOCTYPE html>
<html>
<head>
   <title>To display the date and time of a document when it is last modified in JavaScript</title>
   <meta charset="UTF-8">
   <meta http-equiv="X-UA-Compatible" content="IE=edge">
   <meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
   <p id="last-modified" style="text-align:center"></p>
   </div>
   <script>
      document.getElementById('last-modified').innerHTML = 'The date and time of the document when it is last modified is : '+document.lastModified;
   </script>
</body>
</html>

On executing the above code, the following output is generated.

Example 3

The below program is an example which changes the lastModified property to Date object and to display the last modified date and time in our own format.

<!DOCTYPE html>
<html>
<head>
   <title>To display the date and time of a document when it is last modified in JavaScript</title>
   <meta charset="UTF-8">
   <meta http-equiv="X-UA-Compatible" content="IE=edge">
   <meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
   <p id="last-modified" style="text-align:center"></p>
  </div>
   <script>
      const date = new Date(document.lastModified);
      document.getElementById('last-modified').innerHTML = "Date of modification on "+'<br/>'+date.getDate()+"-"+date.getMonth()+"-"+date.getFullYear()+" at time : "+date.getHours()+" hours "+date.getMinutes()+" minutes and "+date.getSeconds()+' seconds';
   </script>
</body>
</html>

On executing the above code, the following output is generated.

Updated on: 08-Dec-2022

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements