JavaScript Document lastModified Property

Beginner
⏱️ 12 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Baseline Widely available
Instance property

What You’ll Learn

Document.lastModified is an instance property that returns a string with the date and local time when the current document was last modified. Learn how to display it, convert it to a Date, compare timestamps, and five examples with try-it labs.

01

Kind

Instance property

02

Returns

string

03

Means

Last modified

04

Parse

Date / Date.parse

05

Compare

Use numbers

06

Status

Baseline widely

Introduction

Want to show visitors when a page was updated? document.lastModified gives you that information as a readable date/time string in the user’s local time zone.

MDN: the property returns a string containing the date and local time on which the current document was last modified. The value often comes from the server’s Last-Modified information (or a fallback the browser provides).

💡
Compare with numbers, not strings

MDN notes that as a string, lastModified is hard to compare directly. Convert with Date.parse() or new Date() before sorting or checking for updates.

Related Document tutorials: cookie, documentURI, contentType, Document constructor.

Understanding Document.lastModified

An instance property on Document. Its value is a date/time string for the current document.

  • Value — string with last-modified date and local time (MDN).
  • Display — show it directly in the UI, or format a Date object.
  • Parsenew Date(document.lastModified) or Date.parse(...) (MDN).
  • Compare — convert to milliseconds first; do not lexicographically compare strings (MDN).
  • External pages — use fetch HEAD and the Last-Modified header (MDN).

📝 Syntax

JavaScript
document.lastModified

Value

A string (MDN). Example shape from MDN: Tuesday, December 16, 2017 11:09:42 — exact formatting can vary by browser/locale.

MDN: convert to Date

JavaScript
let oLastModif = new Date(document.lastModified);
let nLastModif = Date.parse(document.lastModified);

⚡ Quick Reference

GoalCode / note
Show last modifieddocument.lastModified
As Date objectnew Date(document.lastModified)
As millisecondsDate.parse(document.lastModified)
Locale formatnew Date(document.lastModified).toLocaleString()
Detect page changeCompare with cookie timestamp (MDN)
MDN statusBaseline Widely available (since Jul 2015)

🔍 At a Glance

Four facts about document.lastModified.

Type
string

Date & time

Timezone
local

User local

Compare
Date.parse

Not raw string

Status
baseline

Widely available

📋 String vs Date vs Milliseconds

FormGood forWatch out
Raw lastModified stringQuick displayHard to compare (MDN)
new Date(...)Formatting, calendarsInvalid Date if parse fails
Date.parse(...)Comparisons, cookiesReturns NaN if unparsable

Examples Gallery

Examples follow MDN Document: lastModified. Output times vary by when the file was served or saved.

📚 Getting Started

Read the string and convert it the MDN way.

Example 1 — MDN: Read document.lastModified

Log or display the last-modified string.

JavaScript
console.log(document.lastModified);
// e.g. Tuesday, December 16, 2017 11:09:42
Try It Yourself

How It Works

MDN alerts this value for simple usage. Exact string format depends on the browser.

Example 2 — MDN: Transform into a Date

Build a real Date object from the string.

JavaScript
let oLastModif = new Date(document.lastModified);

console.log(oLastModif.toString());
console.log(oLastModif.getFullYear());
Try It Yourself

How It Works

Once you have a Date, you can format, add days, or compare with other dates.

📈 Milliseconds, Locale & Change Detection

Compare timestamps and show friendly “Updated” text.

Example 3 — MDN: Transform into Milliseconds

Get a numeric timestamp for comparisons.

JavaScript
let nLastModif = Date.parse(document.lastModified);

console.log(nLastModif);
console.log(Number.isNaN(nLastModif) ? "unparsable" : "ok");
Try It Yourself

How It Works

MDN: milliseconds since Jan 1, 1970, 00:00:00 local time — ideal for cookie comparisons.

Example 4 — Friendly Locale Formatting

Show an “Updated” label visitors can read easily.

JavaScript
const updated = new Date(document.lastModified);

console.log(
  "Updated: " + updated.toLocaleString(undefined, {
    dateStyle: "medium",
    timeStyle: "short"
  })
);
Try It Yourself

How It Works

toLocaleString adapts to the visitor’s language and region settings.

Example 5 — MDN Idea: Detect Page Change with a Cookie

Compare current lastModified with a stored visit timestamp.

JavaScript
const pattern = /last_modif\s*=\s*([^;]*)/;
const lastVisit = parseFloat(document.cookie.replace(pattern, "$1"));
const lastModif = Date.parse(document.lastModified);

if (Number.isNaN(lastVisit) || lastModif > lastVisit) {
  document.cookie =
    "last_modif=" + Date.now() +
    "; expires=Fri, 31 Dec 9999 23:59:59 GMT; path=" + location.pathname;

  if (isFinite(lastVisit)) {
    console.log("This page has been changed!");
  } else {
    console.log("First visit — timestamp saved.");
  }
} else {
  console.log("No change since last visit.");
}
Try It Yourself

How It Works

Based on MDN’s cookie pattern (skip first visit). Requires cookies enabled in the try-it environment.

🚀 Common Use Cases

  • Footer “Updated” line — show when the article was last modified.
  • Changelog hints — prompt returning visitors that content changed.
  • Cache debugging — confirm whether a refreshed page looks newer.
  • Static sites — surface file modification time from the server.
  • External monitoring — HEAD + Last-Modified for other URLs (MDN).
  • Admin tools — list page freshness during content reviews.

🧠 How lastModified Is Used

1

Server / browser records modification time

Often tied to when the document file or response was last changed.

Source
2

You read document.lastModified

Gets a local-time date string (MDN).

Read
3

Parse when you need logic

Use Date / Date.parse before comparing or formatting.

Parse
4

Display or detect updates

Show an Updated label, or compare with a cookie / HEAD header.

📝 Notes

  • MDN: Baseline Widely available (since July 2015) — no Deprecated / Experimental / Non-standard banner.
  • Do not compare raw strings for sorting — parse first (MDN).
  • String format can vary by browser; treat it as opaque until parsed.
  • For other URLs, use fetch HEAD and inspect Last-Modified (MDN).
  • Related: cookie, lastElementChild, documentURI, Document constructor.

Universal Browser Support

Document.lastModified is marked Baseline Widely available on MDN (since July 2015). Logos use the shared browser-image-sprite.png sprite from this project.

Baseline · Widely available

Document.lastModified

String with the date and local time when the current document was last modified.

Universal Widely available
Google Chrome Full support · Desktop & Mobile
Full support
Mozilla Firefox Full support · Desktop & Mobile
Full support
Apple Safari Full support · macOS & iOS
Full support
Microsoft Edge Full support · Chromium
Full support
Opera Full support · Modern versions
Full support
Internet Explorer Supported in legacy IE
Full support
Document.lastModified Excellent

Bottom line: Display document.lastModified for an Updated line, or parse with Date / Date.parse before comparing timestamps.

Conclusion

Document.lastModified is a simple way to read when the current page was last modified. Display the string, or parse it into a Date / milliseconds when you need comparisons, cookies, or locale-friendly formatting.

Continue with lastStyleSheetSet, cookie, lastElementChild, or the JavaScript hub.

💡 Best Practices

✅ Do

  • Parse with Date / Date.parse before comparing
  • Use toLocaleString for visitor-friendly Updated labels
  • Store visit timestamps in cookies carefully (path / expiry)
  • Use HEAD + Last-Modified for external pages (MDN)
  • Handle NaN if parsing fails

❌ Don’t

  • Compare raw lastModified strings with > / <
  • Assume every browser formats the string identically
  • Treat it as a precise “content publish date” for SEO without verifying the server
  • Forget that local files / try-it sandboxes may report unexpected times
  • Rely on cookies alone if users block them

Key Takeaways

Knowledge Unlocked

Five things to remember about document.lastModified

When this document was last modified — as a string.

5
Core concepts
📅02

Means

Last modified

Local time
🕐03

Parse

Date / parse

MDN
⚖️04

Compare

Use ms

Not strings
🌐05

External

HEAD header

fetch

❓ Frequently Asked Questions

A string with the date and local time when the current document was last modified (MDN).
No. MDN marks Document.lastModified as Baseline Widely available (since July 2015). It is a standard Document property.
Do not compare the strings directly. Convert with Date.parse(document.lastModified) or new Date(document.lastModified), then compare numbers or Date objects (MDN).
MDN shows storing a timestamp in a cookie and comparing it to Date.parse(document.lastModified) on later visits.
MDN suggests a HEAD request with fetch() and reading the Last-Modified response header.
Browsers return a human-readable date/time string in local time. Always parse it with Date APIs before sorting or comparing.
Did you know?

Opening a page from disk (file://) or from a live editor sandbox can produce different lastModified values than a production URL—always verify on the real server when freshness matters.

Next: lastStyleSheetSet

Learn the deprecated style sheet set property (and modern alternatives).

lastStyleSheetSet →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful