Skip to content Skip to sidebar Skip to footer

LastModified() Function Returns Current Date And Time

My question is : why when I use 'document.lastModified' on my web page, it returns me the current date and time, not the time when this page was last Modified... Any ideas? Thanks

Solution 1:

I conducted an experiment today. I put the following script on my server in a file touched to be last modified on 1 Jan:


var theDate = new Date(document.lastModified).toISOString().substr(5, 5).replace('-', '');
document.write(theDate);

and asked visitors to tell me what they see. 103 said they see "0101", the expected string, 8 said they see "0308", ie the current date. All the bad answers came from Android, Chrome/7[12] users. Good answers (22) also came from other Android users, typically with earlier versions of Chrome. However there were 10 Android Chrome/72 users who saw the good date.

Based on this small sample, there must be something wrong in Android Chrome versions 71+, but it is stochastic.


Solution 2:

Because you are modifying it currently. Check this out for example.

To make this work based on your requirement, checkout this link and this link

Copying from the external link:

Put this on the page at the bottom:

 <script type="text/javascript" src="js_lus.js"></script>

Name the file whatever you want. Example: js_lus.js Make sure src="" path is correct for all your pages.

function lastModified() {
    var modiDate = new Date(document.lastModified);
    var showAs = modiDate.getDate() + "-" + (modiDate.getMonth() + 1) + "-" + modiDate.getFullYear();
    return showAs
}

function GetTime() {
    var modiDate = new Date();
    var Seconds

    if (modiDate.getSeconds() < 10) {
        Seconds = "0" + modiDate.getSeconds();
    } else {
        Seconds = modiDate.getSeconds();
    }

    var modiDate = new Date();
    var CurTime = modiDate.getHours() + ":" + modiDate.getMinutes() + ":" + Seconds
    return CurTime
}

document.write("Last updated on ")
document.write(lastModified() + " @ " + GetTime());
document.write(" [D M Y 24 Hour Clock]")
document.write("");

Solution 3:

i've run into this issue while programming with PHP. in my case, the issue was that since PHP is a server side language, the page got re rendered each time it was called and thus reset the javascript code asking for when it was edited.


Solution 4:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN">
<html><head><title>Last Modified Date</title></head><body>
Last modified:
<script language="JavaScript">
var testlast=document.lastModified;
document.write(" "+testlast.substr(0,10));
</script><br>
</body></html>

That code should return the Last Modified Date of the source document. It always has in the past. The 'document' being changed is the display form of the web page, NOT THE SOURCE. Line 3 should be fetching the document's modification date, which does NOT change before, during, or after the page is displayed. So 'testlast' should be the source's modification date, NOT the current date. This code works fine when I place the source on my Macintosh, and access it from my 'localhost'.


Post a Comment for "LastModified() Function Returns Current Date And Time"