Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
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: Basic lastModified Display
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>
This document is lastly modified on 12/15/2024 10:30:45
Example 2: Using getElementById to Display
Let's look at another example program to display the last modified date and time using DOM manipulation.
<!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>
<script>
document.getElementById('last-modified').innerHTML = 'The date and time of the document when it is last modified is : ' + document.lastModified;
</script>
</body>
</html>
The date and time of the document when it is last modified is : 12/15/2024 10:30:45
Example 3: Custom Date Formatting
The below program is an example which converts the lastModified property to Date object and displays 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>
<script>
const date = new Date(document.lastModified);
document.getElementById('last-modified').innerHTML = "Date of modification on " + '<br/>' + date.getDate() + "-" + (date.getMonth() + 1) + "-" + date.getFullYear() + " at time : " + date.getHours() + " hours " + date.getMinutes() + " minutes and " + date.getSeconds() + ' seconds';
</script>
</body>
</html>
Date of modification on 15-12-2024 at time : 10 hours 30 minutes and 45 seconds
Key Points
- The
document.lastModifiedproperty returns a string representation of the last modification date - You can convert this string to a Date object for custom formatting using
new Date(document.lastModified) - The month value from
getMonth()starts from 0, so add 1 for correct display - This property is useful for showing users when content was last updated
Conclusion
The document.lastModified property provides an easy way to display when a web document was last updated. You can use it directly or format it using Date object methods for better presentation.
