I set up the following code to check how many days have elapsed since a file has been uploaded:
Code:
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.
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]) );
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
<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.
He wanted the value in a JS Date object so he could get the "days since modified" value.
Okay, the full code:
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=..."
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
He wanted the value in a JS Date object so he could get the "days since modified" value.
Sorry, it doesn't.
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";
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.
Code:
<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>