Chengzhi Chengzhi - 3 months ago 19
HTML Question

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.

Answer

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();
}
Comments