Chengzhi
Chengzhi

Reputation: 2591

How to see the last modified HTML date by JavaScript

all,

I have an issue with JavaScript to show the last modified date of .html file. I tried to put the last modified date code in my footer, thus I do not need to write those code anymore. And it will show the last modified date of each page.

I tried the code on the website:

<html>
<head>
<title>Last Modified</title>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1">
<script type="text/javascript">

function showLastModified() {
var out = document.getElementById('lastModified');
var d = new Date();
if (d.toLocaleDateString) {
out.innerHTML = d.toLocaleDateString(document.lastModified);
}
else {
out.innerHTML = document.lastModified;
}
}

window.onload = showLastModified;
</script>
</head>
<body>
Last Modified on <span id="lastModified">&nbsp;</span>
</body>
</html>

However, it still show the date of today. I think it is because of d.toLocaleDateString, but I can not figure out. Can anyone help me resolve my problem.

Upvotes: 2

Views: 10520

Answers (3)

willy wonka
willy wonka

Reputation: 1686

I've just tried to verify document.lastModified on both local html/js file and on remote snippet file. It seems to me the behavior is different: on local file it is returning the system date and time NOT the html file last modified date and time as expected. It is very odd behavior. How I checked this: I have inserted the command into the js then I looked up into the document property at run time with google chrome dev tools in debug interruption/break point mode: every time I point the mouse onto document.lastmodified property it changes accordingly with the system time and updates every second, without exiting the html document, without saving it elsewhere and reloading into the browser, without doing anything else to it with any editor. Then I deactivated the break point and I let the code execute freely and it returns the system time too... On remote file instead it works properly as expected and returns the last save date of the html file it self and it is not dynamic.

This double check lets me think that the command is not correctly interpreted from the browser if the file is a local one. Is the command document.lastModified some way "broken" for local files?

Can someone else check this odd behavior, please?

Edit:

The odd behavior is the same on opera, while firefox works as expected in both local and online html files.

    document.write(document.lastModified);

Upvotes: 0

kennebec
kennebec

Reputation: 104770

Most browsers support document.lastModified. but you need to turn the string into a Date object before calling toLocaleString-

if(document.lastModified){
    document.getElementById('lastModified').innerHTML=
    new Date(document.lastModified).toLocaleString();
}

Upvotes: 1

epascarello
epascarello

Reputation: 207501

toLocaleDateString has no parameters.

var d = new Date( document.lastModified );
console.log( d.toLocaleDateString() );

Upvotes: 1

Related Questions