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
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
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.
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.
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.
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; } }
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:
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.
<!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
With just plain HTML, no you cannot.
You can with PHP, or ASP, or any other server side language.
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.