document.lastModified returns a string containing the date and local time that the browser reports for the current document's last modification.
const modified = document.lastModified;
console.log(modified);
const value = document.lastModified;
console.log(typeof value); // "string"
The original tutorial used document.write() to display this value. For normal page output, assign the text to an existing element instead.
When the string is parseable, create a Date object:
const modifiedDate = new Date(document.lastModified);
if (!Number.isNaN(modifiedDate.getTime())) {
console.log(modifiedDate);
}
See the JavaScript Date object tutorial for date methods and formatting.
The legacy example manually joined day, month, and year and accidentally used the zero-based month directly. A safer display is:
const modifiedDate = new Date(document.lastModified);
const formatted = modifiedDate.toLocaleString();
console.log(formatted);
This value depends on information available to the browser. It may reflect server response metadata or file information, but it is not a guaranteed content-version system and should not be treated as authoritative publishing metadata.
For a site that needs reliable content dates, store and render those dates from the content or server-side publishing system rather than inventing them from the browser value.
const modifiedMs = Date.parse(document.lastModified);
if (!Number.isNaN(modifiedMs)) {
console.log(modifiedMs);
}
Comparing timestamps is more reliable than comparing formatted date strings.
document Object document.write()Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.