...

View Full Version : Get date of file - What am I doing wrong?



salmon trout
11-21-2010, 09:42 PM
I set up the following code to check how many days have elapsed since a file has been uploaded:


function fileModified(url){
try{
var Oxml= new window.XMLHttpRequest();
Oxml.open("HEAD", url, false);
Oxml.send(null);
if(Oxml.status== 200){
var lastmod = Oxml.getResponseHeader('Last-Modified');
return lastmod;
}
else return null;
}
catch(er){
return null;
}
}
modified = fileModified(location.href);
var now = new Date();
var msecsPerDay = 1000 * 60 * 60 * 24;
var msecsDiff = now.getTime() - modified.getTime();
var diff= msecsDiff / msecsPerDay ;

alert("Days since file was uploaded="+diff);

My problem is that modified.getTime() doesn't work. Apparently the result of Oxml.getResponseHeader('Last-Modified') is a string, such as Sun, 21 Nov 2010 21:17:49 GMT.

I tried and tried - but couldn't get to make a proper date out of this.

The final result that I need is the time since the file was uploaded.

Thanks!

Old Pedant
11-21-2010, 10:01 PM
JUst have to parse the string.



var lastmod = "Sun, 21 Nov 2010 21:17:49 GMT";
var parts = lastmod.split(" ");
var mos = "janfebmaraprmayjunjulaugsepoctnovdec";
var day = parseInt( parts[1] ); // 21 from above
var month = mos.indexOf( parts[2].toLowerCase() ) / 3; // nov would be indexOf==30
var year = parseInt( parts[3] );
var time = parts[4].split(":"); // split apart the time
var jsLastMod = new Date( year, month, day, parseInt(time[0]), parseInt(time[1]), parseInt(time[2]) );

mrhoo
11-22-2010, 12:40 AM
Never mind- I gave my 2 cents before I saw you have been posting the same question on every forum you could find.

Old Pedant
11-22-2010, 12:58 AM
Cross posting is EVIL. Wish I'd know about that before I bothered replying.

Philip M
11-22-2010, 07:50 AM
JUst have to parse the string.


That code seems to do nothing. Suggest:-


<script type = "text/javascript">

var lastmod = "Sun, 21 Nov 2010 21:17:49 GMT";
var parts = lastmod.split(" ");
var mos = "JanFebMarAprMayJunJulAugSepOctNovDec";
var day = parseInt( parts[1] ); // 21 from above
var monthnum = mos.indexOf(parts[2]); // Nov would be indexOf==30
month = mos.substr(monthnum,3);
monthnum = monthnum/3 +1;
var year = parseInt( parts[3] );
var time = parts[4].split(":"); // split apart the time
var dta = day + "/" + monthnum + "/" + year + " " + parseInt(time[0]) + ":" + parseInt(time[1]) + ":" + parseInt(time[2]);
alert (dta);
var dtb = day + " " + month + " " + year + " " + parseInt(time[0]) + ":" + parseInt(time[1]) + ":" + parseInt(time[2]);
alert (dtb);

</script>

salmon trout - If you post in multiple forums you will make yourself very unpopular, and find that you may not get an answer in any of them. People do not want to waste time answering a question when it has perhaps already been answered elsewhere. People who have a record of cross posting will tend to be ignored.
There are few things in forums more irritating than taking the time to unsnarl someone's markup, javascript and css, figure out the fix, and post it only to find you've wasted your time because a duplicate post in another forum has already been answered. This prohibition on cross-posting is a rule in almost every forum.

Old Pedant
11-22-2010, 06:55 PM
??? What ???

That code worked fine.

He wanted the value in a JS Date object so he could get the "days since modified" value.

Okay, the full code:


var lastmod = "Sun, 21 Nov 2010 21:17:49 GMT";
var parts = lastmod.split(" ");
var mos = "janfebmaraprmayjunjulaugsepoctnovdec";
var day = parseInt( parts[1] ); // 21 from above
var month = mos.indexOf( parts[2].toLowerCase() ) / 3; // nov would be indexOf==30
var year = parseInt( parts[3] );
var time = parts[4].split(":"); // split apart the time
var jsLastMod = new Date( year, month, day, parseInt(time[0]), parseInt(time[1]), parseInt(time[2]) );

var now = new Date();
var msecsPerDay = 1000 * 60 * 60 * 24;
var msecsDiff = now.getTime() - jsLastMod.getTime();
var diff= msecsDiff / msecsPerDay ;

alert("Days since file was uploaded="+diff);

And it works fine. I just didn't bother to show the stuff starting at "now=..."

Philip M
11-22-2010, 07:52 PM
??? What ???

That code worked fine.

He wanted the value in a JS Date object so he could get the "days since modified" value.



Sorry, it doesn't. :p

document.write(jsLastMod) gives:-
Sun Nov 21 21:17:49 UTC 2010
which (apart form the GMT) is what you had to start with.
var lastmod = "Sun, 21 Nov 2010 21:17:49 GMT";

Old Pedant
11-22-2010, 08:10 PM
LOL! Well, yes, but that wasn't the point.

The problem was that he wasn't able to do

var msecsDiff = now.getTime() - modified.getTime();

So the interesting part isn't doing document.write of the modified variable, at all.

Logic Ali
11-22-2010, 09:06 PM
My problem is that modified.getTime() doesn't work. Apparently the result of Oxml.getResponseHeader('Last-Modified') is a string, such as Sun, 21 Nov 2010 21:17:49 GMT.Fortunately the string is in a format that can be paresed by the Date constructor, so all that's needed is to create a Date object using it and subtract it from the present.
<script type='text/javascript'>

function fileModified( url, func )
{
var retVal = null, error = false;

try
{
var rq = XMLHttpRequest();

rq.open("HEAD", url, true);

rq.onreadystatechange = function()
{
if( this.readyState == 4 && ( retVal = this.getResponseHeader( 'Last-Modified' ) ) )
func( retVal );
}

rq.send( null );
}
catch( er ){ error = true; }

return error;
}

function showModDate( dateStr )
{
alert( dateStr + '\n\n' + Math.floor( ( new Date() - new Date( dateStr ) ) / 86400000 ) + ' days' );
}

fileModified( location.href, showModDate ); // Header not returned for .php files

</script>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum