www.webdeveloper.com
Page 2 of 2 FirstFirst 12
Results 16 to 18 of 18

Thread: Extract Date and Time Stamp of a file/image

  1. #16
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,264
    Eureka ! I found how to obtain a date with Opera ...
    To avoid that the browser tries to read the image lastModified in the cache, you have only to call
    Code:
    dt=LastModUsingHeader('upload.jpg?'+Math.random()); 
    // or to modify the function 
    function LastModUsingHeader(U) {
        var X = !window.XMLHttpRequest ? new ActiveXObject('Microsoft.XMLHTTP') : new XMLHttpRequest;
        X.open('GET', U+'?'+Math.random(), false);
       	try{ X.send();}catch(y){}
      	var dt=X.getResponseHeader('Last-Modified'); 
    	return new Date(dt).toLocaletring(); 
    }
    // to write
    <p>Image uploaded: <i id="ImageDate"></i></p>
    
    <script type="text/javascript">
    document.getElementById('ImageDate').innerHTML=LastModUsingHeader('upload.jpg'); 
    </script>
    This could be applied too to the image src...
    It would be better to have a DOCTYPE for the page (See W3C) and to display meta tags to control the cache (See this page) to choose a Meta tag...
    HTML Code:
    <META HTTP-EQUIV="CACHE-CONTROL" CONTENT="NO-CACHE">
    <META HTTP-EQUIV="PRAGMA" CONTENT="NO-CACHE">
    Otherwise the users could have difficulties to update the page...
    Last edited by 007Julien; 11-16-2011 at 04:57 AM.

  2. #17
    Join Date
    Nov 2011
    Posts
    4

    resolved

    Thanks to all!

    This is the code I'm using and it seems to work for Opera, Firefox and IE. The date/time stamp of a file shows up correctly at local time:

    Code:
    <p>Image uploaded: <i id="getImageDate"></i></p>
    
    <script type="text/javascript">
    function LastModUsingHeader(sFile) {
    	var X = !window.XMLHttpRequest ? new ActiveXObject('Microsoft.XMLHTTP') : new XMLHttpRequest;
        X.open('GET', sFile, false);
       	try{ X.send();}
    	catch(y){}
      	var dtImage=new Date();
    	dtImage = X.getResponseHeader('Last-Modified'); 
    	return new Date(dtImage).toLocaleString();
    }
    
    document.getElementById('getImageDate').innerHTML=LastModUsingHeader('upload.jpg'); ;
    	
    </script>

  3. #18
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,264
    It's could be better to call (See for example this page)
    Code:
    document.getElementById('getImageDate').innerHTML=LastModUsingHeader('upload.jpg?avoidCache='+Math.random()));
    For me it's do not work with Opera without this addition.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles