9

Is there a way to get the Last-Modified-Date of a file on a Web Site?

i.e. Here is an example file I have out there: http://www.ymcadetroit.org/atf/cf/%7B2101903E-A11A-4532-A64D-9D823368A605%7D/Birmingham_Youth_Sports_Parent_Manual.pdf

Gerhard Weiss
  • 9,343
  • 18
  • 65
  • 67
  • 1
    Can you clarify? What needs to get the time? A client program? Something on the server? A browser script? – Eddie Deyo Jun 23 '09 at 20:14
  • Possible duplicate of [How do I find when a web page was last updated?](http://stackoverflow.com/questions/23644436/how-do-i-find-when-a-web-page-was-last-updated) – Martin Thoma Jan 06 '17 at 10:47
  • @MartinThoma: This is the older post - it is the other way round... – strpeter Aug 17 '18 at 08:16
  • I don't think age matters: https://meta.stackoverflow.com/q/315472/562769 - the older one can be marked as a duplicate of the newer one. To me, it helps to realize that closing as duplicate is NOT meant as an insult / punishment. It's just a form of organization. Most importantly of the answers. – Martin Thoma Aug 17 '18 at 08:59
  • For equivalent Java code, refere to this [post](https://stackoverflow.com/questions/7999258/get-the-last-modified-date-of-an-url) – Mehdi Sep 27 '18 at 02:12

10 Answers10

9

Go to the website you want to know about, wait for it to fully load, then go to the address bar and write this:

javascript:alert(document.lastModified)

You'll get a popup that says when it was last modified.

Tunaki
  • 132,869
  • 46
  • 340
  • 423
  • If I try that on, for instance, this page, it tells me the current time - so if I enter that in the URL bar twice it gives two different times, which can't be true because the file was only loaded once! – Michael Dec 16 '16 at 02:11
7

The HTTP intends the Last-Modified header field to declare the last modification date. But the server needs to know that date.

On static files whose content is sent directly to the client and not interpreted otherwise by the server (e.g. .html, .css, .js) it uses the last modified date of that file. But on files that generated content dynamically (PHP, Python, etc.) the script needs to specify that information itself. But unfortunatly many scripts don’t to that.

So if a Last-Modified header field is present, you can use that information. But if not, you cannot determin the last modification date.

Gumbo
  • 643,351
  • 109
  • 780
  • 844
5

Here is some C# code to do it:

public DateTime GetLastModifyTime(string url)
{
        WebRequest request = WebRequest.Create(url);
        request.Credentials = CredentialCache.DefaultNetworkCredentials;
        request.Method = "HEAD";

        using (WebResponse response = request.GetResponse())
        {
            string lastModifyString = response.Headers.Get("Last-Modified");
            DateTime remoteTime;
            if (DateTime.TryParse(lastModifyString, out remoteTime))
            {
                return remoteTime;
            }

            return DateTime.MinValue;
        }
}
Eddie Deyo
  • 5,200
  • 8
  • 35
  • 35
5

I realize this question is 4 years old, but a search of the web proved that satisfactory answers remain rare. Peter's answer is part of the solution. When I had the same problem to solve, that got me started. But the rest of the solution...

As he said, the web server must be configured to send the last-modified date ... so how do you configure the web server?

Assuming you have the necessary level of control, you first need to enable server side includes. There are several ways to do this - one of which is the "xbithack". A good reference is http://httpd.apache.org/docs/current/howto/ssi.html.

Assuming you've done this, you need to set the execute bit on any html file that needs to have server-side includes parsed. This can be done at the command line of a UNIX-like system: chmod u+x file.html or on the Mac using get-info (command-I) on the file.

This leaves the snippet to actually put in your file, which looks like this:

This document last modified <!--#flastmod file="index.html" -->

Since I found many, many recommendations that didn't include this, and simply used the javascript document.lastModified, I suspect that some servers give you what you want with the javascript version, whereas some (including the one hosting our stuff) don't.

DRVic
  • 2,481
  • 1
  • 15
  • 22
3

To obtain the last modified date from client side, you can access the HTML DOM using the lastModified property using JavaScript.

The lastModified property grabs the information from the head portion sent with all web requests. The value can be manually set by developers on the web-server side of things so it may not reflect the actual last modified date of the file responsible for delivering the content.

Example

<!DOCTYPE html>
<html>
<body>
  
<b>document.lastModified : </b>
<script>document.write( document.lastModified );</script>

</body>
</html>

The specific command in JavaScript that retrieves this is document.lastModified and can easily be converted into a Date object as follows :

var x = new Date(document.lastModified);

More information can be found on the site I used as a reference w3 schools : HTML DOM lastModified Property

Kraang Prime
  • 9,981
  • 10
  • 58
  • 124
  • The only reason I can see this being down-voted is for being "too short". From client-side only, this is definitely the most elegant method, and proof of this command being viable can be found [here on w3schools](http://www.w3schools.com/jsref/prop_doc_lastmodified.asp) - making some edits to this to make the solution more viable. – Kraang Prime Dec 14 '16 at 19:29
2

I believe the web server must be configured to send the last-modified date in an HTTP-header, this is certainly one way. Check out section 14.29 Last-Modified of this document:

http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html

Peter
  • 6,354
  • 1
  • 31
  • 29
2

You can do the following to get Last-Modified: https://superuser.com/a/991895

Using curl:

curl -s -v -X HEAD http://foo.com/bar/baz.pdf 2>&1 | grep '^< Last-Modified:'

Using wget:

wget --server-response --spider http://example.com/bar/example.pdf 2>&1 | grep -i Last-Modified
0

With just plain HTML, no you cannot.

You can with PHP, or ASP, or any other server side language.

jgallant
  • 11,143
  • 1
  • 38
  • 72
0

I'm not an expert in headers, but believe you are looking for this:

There is a way to check the date when a file was modified: View HTTP headers in Google Chrome?

Check in there (Chrome's Developer Tools / Network / Selected File / Headers) the "If-Modified-Since" variable.

Until now this has helped me to achieve what you are asking, get a file's modification date.

Community
  • 1
  • 1
DavidTaubmann
  • 3,223
  • 2
  • 34
  • 43
  • While that link may provide a answer to the question, it would be better if you could provide the essential parts of the solution in your answer, with proper attribution. Otherwise, your answer may turn invalid or useless if the link becomes broken or the page's content changes. – JW Lim Jun 04 '14 at 01:40
-2

In php:

print getlastmod();
print gmdate('D, d M Y H:i:s', getlastmod());
Igor Tarasov
  • 27
  • 2
  • 11