JavaScript document.lastModified: Read a Page Modification Time

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);
Browser-reported value for this page:
Table of Contents

Read document.lastModified Top ↑

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.

Convert the Value to a Date Object Top ↑

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.

Format the Modification Date Top ↑

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);

What document.lastModified Can and Cannot Tell You Top ↑

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.

Compare Parsed Times, Not Display Strings Top ↑

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()


Subscribe to our YouTube Channel here



plus2net.com










✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer