Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-21-2010, 09:42 PM   PM User | #1
salmon trout
New to the CF scene

 
Join Date: Nov 2010
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
salmon trout can only hope to improve
Get date of file - What am I doing wrong?

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.

Thanks!
salmon trout is offline   Reply With Quote
Old 11-21-2010, 10:01 PM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,575
Thanks: 62
Thanked 4,062 Times in 4,031 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
JUst have to parse the string.

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]) );
__________________
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.
Old Pedant is online now   Reply With Quote
Old 11-22-2010, 12:40 AM   PM User | #3
mrhoo
Regular Coder

 
Join Date: Mar 2006
Posts: 710
Thanks: 31
Thanked 128 Times in 119 Posts
mrhoo will become famous soon enoughmrhoo will become famous soon enough
Never mind- I gave my 2 cents before I saw you have been posting the same question on every forum you could find.

Last edited by mrhoo; 11-22-2010 at 12:44 AM..
mrhoo is offline   Reply With Quote
Old 11-22-2010, 12:58 AM   PM User | #4
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,575
Thanks: 62
Thanked 4,062 Times in 4,031 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Cross posting is EVIL. Wish I'd know about that before I bothered replying.
__________________
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.
Old Pedant is online now   Reply With Quote
Old 11-22-2010, 07:50 AM   PM User | #5
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,103
Thanks: 197
Thanked 2,421 Times in 2,399 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by Old Pedant View Post
JUst have to parse the string.
That code seems to do nothing. Suggest:-

Code:
<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.

Last edited by Philip M; 11-22-2010 at 07:58 AM..
Philip M is offline   Reply With Quote
Old 11-22-2010, 06:55 PM   PM User | #6
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,575
Thanks: 62
Thanked 4,062 Times in 4,031 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
??? 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:
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.
Old Pedant is online now   Reply With Quote
Old 11-22-2010, 07:52 PM   PM User | #7
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,103
Thanks: 197
Thanked 2,421 Times in 2,399 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by Old Pedant View Post
??? 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.

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";
Philip M is offline   Reply With Quote
Old 11-22-2010, 08:10 PM   PM User | #8
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,575
Thanks: 62
Thanked 4,062 Times in 4,031 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
LOL! Well, yes, but that wasn't the point.

The problem was that he wasn't able to do
Code:
var msecsDiff = now.getTime() - modified.getTime();
So the interesting part isn't doing document.write of the modified variable, at all.
__________________
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.
Old Pedant is online now   Reply With Quote
Old 11-22-2010, 09:06 PM   PM User | #9
Logic Ali
Regular Coder

 
Logic Ali's Avatar
 
Join Date: Sep 2010
Location: London
Posts: 976
Thanks: 0
Thanked 203 Times in 198 Posts
Logic Ali will become famous soon enoughLogic Ali will become famous soon enough
Quote:
Originally Posted by salmon trout View Post
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>
Logic Ali is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:47 AM.


Advertisement
Log in to turn off these ads.