0

I've just tried document.lastModified but 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 without 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 as the first check...

This double check lets me think that the command is not correctly interpreted from the browser anymore.

Is the command document.lastModified some way "broken"?

EDIT: The file I'm talking about is a local not a server file, so no http requests or headers involved.

..._ _ _...

willy wonka
  • 1,440
  • 1
  • 18
  • 31
  • http://stackoverflow.com/questions/20665656/javascript-document-lastmodified-is-returning-current-date-and-time – Netorica Aug 08 '16 at 04:30
  • Possible duplicate of [lastModified() function returns current date and time](http://stackoverflow.com/questions/16024346/lastmodified-function-returns-current-date-and-time) – James Aug 08 '16 at 04:31
  • @OlgaReal, into the linked topic is discussed about server file, but mine is a local file, sorry I didn't specified before, I edited the main question of this topic to specify better this aspect. – willy wonka Aug 08 '16 at 04:34

1 Answers1

0

According to the spec:

The Document's source file's last modification date and time must be derived from relevant features of the networking protocols used, e.g. from the value of the HTTP Last-Modified header of the document, or from metadata in the file system for local files.

So it should be there. This is a reported bug in Chrome. Searching the Chromium source code shows that this is indeed the case (note the FIXME comment):

// http://www.whatwg.org/specs/web-apps/current-work/#dom-document-lastmodified
String Document::lastModified() const
{
    DateComponents date;
    bool foundDate = false;
    if (m_frame) {
        if (DocumentLoader* documentLoader = loader()) {
            const AtomicString& httpLastModified = documentLoader->response().httpHeaderField(HTTPNames::Last_Modified);
            if (!httpLastModified.isEmpty()) {
                date.setMillisecondsSinceEpochForDateTime(convertToLocalTime(parseDate(httpLastModified)));
                foundDate = true;
            }
        }
    }
    // FIXME: If this document came from the file system, the HTML5
    // specificiation tells us to read the last modification date from the file
    // system.
    if (!foundDate)
        date.setMillisecondsSinceEpochForDateTime(convertToLocalTime(currentTimeMS()));
    return String::format("%02d/%02d/%04d %02d:%02d:%02d", date.month() + 1, date.monthDay(), date.fullYear(), date.hour(), date.minute(), date.second());
}

Firefox and IE seem to have implemented this, though :)

cdrini
  • 989
  • 10
  • 17
  • Found the line where Firefox does it: https://hg.mozilla.org/mozilla-central/file/tip/dom/base/nsDocument.cpp#l8662 – cdrini Aug 08 '16 at 05:11
  • **"this is a bug in Chrome"**: oh... understood, in this case I sent a msg to chrome devs to beg them correct the bug... hope they will correct it asap – willy wonka Aug 08 '16 at 05:17
  • Check out the bug report here: https://bugs.chromium.org/p/chromium/issues/detail?id=339014 . Out of curiosity, why do you need to access lastModified on a local file? – cdrini Aug 08 '16 at 05:20
  • cause I'm trying to develop a local app that uses html + js code and I need lastModified to show the "file version" for witch I use the date and time instead a number that doesn't give me enough infos about. I prefer to show the date and time to have a precise idea about how recent is the new version of the application file instead of a version number that doesn't tell this info. – willy wonka Aug 08 '16 at 05:27
  • Ah, ok. Thanks for letting me know! – cdrini Aug 08 '16 at 05:32
  • Also the date is evaluated to make certain automatic updates to the page once loaded if it is older than a certain period of time. – willy wonka Aug 08 '16 at 05:40
  • 1
    For anyone stumbling above this one: Three years later, it's still not fixed. – kaiser Jun 04 '19 at 07:59