Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New to the CF scene
    Join Date
    Nov 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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!

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    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.

  • #3
    Regular Coder
    Join Date
    Mar 2006
    Posts
    725
    Thanks
    35
    Thanked 132 Times in 123 Posts
    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.

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    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.

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,734
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    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.

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    ??? 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.

  • #7
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,734
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    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";

  • #8
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    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.

  • #9
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    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>


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •