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.
Page 1 of 4 123 ... LastLast
Results 1 to 15 of 50
  1. #1
    New Coder
    Join Date
    May 2012
    Posts
    30
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Need help with last-modified

    Hello...I need to display the last modified date of each file on my webpage. There are a variety of files including, .doc, .pdf, .ppt, .zip, etc. I am able to get the last modified date to work, however I would like to know if it is possible to format the output of the date/time?

    I have the following code in the head of my html file:
    Code:
    <script type="text/javascript">
    function getlastmod(what)
    {
      var http=new XMLHttpRequest()
      http.open('HEAD',what,false)
      http.send(null)
      if (http.status!=200) return undefined
      return http.getResponseHeader('Last-modified')
    }
    </script>
    In the body section, I have the following:

    Code:
    	    <ul>
    	      <li><a href="documents/OMB_Memo_Conferences_Sep_2011.pdf" target="_blank"> OMB Memo Conferences</a>  <script type="text/javascript">document.write(getlastmod('documents/OMB_Memo_Conferences_Sep_2011.pdf'))</script> <br /></li>
    	      <li><a href="documents/NavOceano_Crb.doc" target="_blank">Nav Oceano CRB</a> <script type="text/javascript">document.write(getlastmod('documents/NavOceano_Crb.doc'))</script><br />
            </ul>
    The output is:

    OMB Memo Conferences Fri, 11 May 2012 13:00:49 GMT
    Nav Oceano CRB Fri, 11 May 2012 13:00:38 GMT

    Is there any way to format the last modified date/time to display just the following:

    OMB Memo Conferences May 11, 2012
    Nav Oceano CRB May 11, 2012

    Any help would be appreciated.
    Thanks,
    Andy

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,032
    Thanks
    203
    Thanked 2,539 Times in 2,517 Posts
    Code:
    <script type = "text/javascript">
    
    var str = "OMB Memo Conferences Fri, 11 May 2012 13:00:49 GMT";
    var len = str.length;
    str = str.substring(0, len - 13);
    alert (str);
    var str = "Nav Oceano CRB Fri, 11 May 2012 13:00:38 GMT";
    var len = str.length;
    str = str.substring(0, len - 13);
    alert (str);
    
    </script>
    I expect that you realise that any document.write() statement that runs after the page finishes loading will create a new page and overwrite all of the content of the current page (including the Javascript which called it).

    All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #3
    New Coder
    Join Date
    May 2012
    Posts
    30
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Here is the page in question, you can see the date being passed beyond the icon.

    http://f150.atwebpages.com/index.html

    Instead of the date being shown as is, I need to format it as described above.

    Andy
    Last edited by lewis712; 05-11-2012 at 07:10 PM.

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,032
    Thanks
    203
    Thanked 2,539 Times in 2,517 Posts
    Code:
    <script type = "text/javascript">
    
    var str = "Fri, 11 May 2012 13:00:49 GMT";
    str = str.replace(/^[^0-9]+/gi,"");  // delete characters until first digit encountered
    var len = str.length;
    str = str.substring(0, len - 13);  // delete the final 13 characters
    alert (str);
    
    var str = "Sat, 12 May 2012 13:00:38 GMT";
    str = str.replace(/^[^0-9]+/gi,"");
    var len = str.length;
    str = str.substring(0, len - 13);
    alert (str);
    
    </script>
    Last edited by Philip M; 05-11-2012 at 07:32 PM. Reason: Improved

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #5
    New Coder
    Join Date
    May 2012
    Posts
    30
    Thanks
    4
    Thanked 0 Times in 0 Posts
    I don't need an "alert" to tell me the date of the file. I need to have it display on the webpage itself.

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,032
    Thanks
    203
    Thanked 2,539 Times in 2,517 Posts
    Quote Originally Posted by lewis712 View Post
    I don't need an "alert" to tell me the date of the file. I need to have it display on the webpage itself.
    A bit curt! The alert is simply for testing and to show you the finished output. I assumed you could work out how to display the result on your page in substitution for the current output. Surely you can modify your getlastmod() function to truncate the result using the code I have provided?

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #7
    New Coder
    Join Date
    May 2012
    Posts
    30
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Philip M View Post
    A bit curt! The alert is simply for testing and to show you the finished output. I assumed you could work out how to display the result on your page in substitution for the current output. Surely you can modify your getlastmod() function to truncate the result using the code I have provided?
    I'm sorry, but I'm not trying to be curt. I'm simply asking for help. I have spend 3 days on trying to get it to reformat. Everything I have tried doesn't pan out. I can't get anything to truncate to this function. I'm pulling my hair out right now.

    Andy

  • #8
    New Coder
    Join Date
    May 2012
    Posts
    30
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Another problem is that on the real html page, I have 500 documents listed out in various sections. I can't have 500 "var" sections in the head of my html file. I will go nuts once a file is changed and I have to update that in the js.

    Andy

  • #9
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,032
    Thanks
    203
    Thanked 2,539 Times in 2,517 Posts
    A little courtesy ('please' and 'thank you') can often smooth your path when requesting help from volunteers. You are not entitled to receive assistance, you know.
    Try this - but I am now closing down my computer for the weekend.


    Code:
    <script type="text/javascript">
    function getlastmod(what) {
    var http=new XMLHttpRequest();
    http.open('HEAD',what,false);
    http.send(null);
    if (http.status!=200) {return undefined}
    var str = http.getResponseHeader('Last-modified');
    
    str = str.replace(/^[^0-9]+/gi,"");  // delete characters until first digit encountered
    var len = str.length;
    str = str.substring(0, len - 13);  // delete the final 13 characters
    return str;
    }
    
    </script>

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • Users who have thanked Philip M for this post:

    lewis712 (05-11-2012)

  • #10
    New Coder
    Join Date
    May 2012
    Posts
    30
    Thanks
    4
    Thanked 0 Times in 0 Posts
    THANK YOU!

    I thank you for your help and apologize for previous posts. I don't know why I was unable to get it to work. I now need to try and get the code to reformat again to read "May 11, 2012".

    Andy

  • #11
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,032
    Thanks
    203
    Thanked 2,539 Times in 2,517 Posts
    Quote Originally Posted by lewis712 View Post
    THANK YOU!

    I thank you for your help and apologize for previous posts. I don't know why I was unable to get it to work. I now need to try and get the code to reformat again to read "May 11, 2012".

    Andy
    Apologies accepted. You are lucky - I shut down my computer but had to reboot it as my wife wanted to read her mail.

    Code:
    <script type = "text/javascript">
    
    var str = "Fri, 11 May 2012 13:00:49 GMT";
    str = str.replace(/^[^0-9]+/gi,"");
    var len = str.length;
    str = str.substring(0, len - 13);
    alert (str);  // for testing
    var strsplit = str.split(" ");
    var reformatted = strsplit[1] + " " + strsplit [0] + " " + strsplit[2];
    alert (reformatted);
    
    </script>

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • Users who have thanked Philip M for this post:

    lewis712 (05-11-2012)

  • #12
    New Coder
    Join Date
    May 2012
    Posts
    30
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Awesome. Again, I apologize. Stress had the best of me. First time I've ever had to post to a forum. I will be better in the future.

    Andy

  • #13
    New Coder
    Join Date
    May 2012
    Posts
    30
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Are you able to see the added dates on my page? I am testing in IE 7, 8 and 9, but one user who has IE 8 said they don't see the date.

    Andy

  • #14
    New Coder
    Join Date
    May 2012
    Posts
    30
    Thanks
    4
    Thanked 0 Times in 0 Posts
    One other thing. Sorry for all the questions.

    I have this for every instance on the page:
    Code:
    <li><a href="documents/OMB_Memo_Conferences_Sep_2011.pdf" target="_blank"> OMB Memo Conferences</a>  <script type="text/javascript">document.write(getlastmod('documents/OMB_Memo_Conferences_Sep_2011.pdf'))</script> <br /></li>
    Now, if i have 500 documents on a page, I will have to create the <script document.write> reference 500 times. Any way of making it global so I only have to call it once?

    Have a great weekend!

    THANKS!
    Andy

  • #15
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,969
    Thanks
    56
    Thanked 557 Times in 554 Posts
    it seems to me like you could save a lot of repetition (and avoid document.write) by doing something like this (untested but should work, as long as you put the script after the html in the body):

    Code:
    <body>
    
    <ul id="doclist">
    	      <li><a href="documents/OMB_Memo_Conferences_Sep_2011.pdf" target="_blank"> OMB Memo Conferences</a></li>
    	      <li><a href="documents/NavOceano_Crb.doc" target="_blank">Nav Oceano CRB</a></li>
            </ul>
    	<script type="text/javascript">
    function getlastmod(what) {
    var http=new XMLHttpRequest();
    http.open('HEAD',what,false);
    http.send(null);
    if (http.status!=200) {return undefined}
    var str = http.getResponseHeader('Last-modified');
    
    str = str.replace(/^[^0-9]+/gi,"");  // delete characters until first digit encountered
    var len = str.length;
    str = str.substring(0, len - 13);  // delete the final 13 characters
    return str;
    }
    
    var lis=document.getElementById("doclist").getElementsByTagName("li")
    for (var i = 0; i < lis.length; i++) {
    lis[i].innerHTML+=getlastmod(lis[i].getElementsByTagName("a")[0].href)
    }
    </script>	
    </body>


  •  
    Page 1 of 4 123 ... LastLast

    Posting Permissions

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