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 7 of 7
  1. #1
    New Coder
    Join Date
    Nov 2012
    Posts
    12
    Thanks
    4
    Thanked 0 Times in 0 Posts

    trying to get day from date last modified

    I need to get the day (Monday, Tuesday, etc) from date last modified code.

    This code is working at the moment but not in htye correct format.

    I would appreciate some help please


    Code:
    <script 
      type="text/JavaScript" 
      language="JavaScript">
    <!-- 
    //
    // format date as dd-mmm-yy
    // example: 12-Jan-99
    //
    function date_ddmmmyy(date)
    {
      var d = date.getDate();
      var m = date.getMonth() + 1;
      var y = date.getYear();
    
      // handle different year values 
      // returned by IE and NS in 
      // the year 2000.
      if(y >= 2000)
      {
        y -= 2000;
      }
      if(y >= 100)
      {
        y -= 100;
      }
    
      // could use splitString() here 
      // but the following method is 
      // more compatible
      var mmm = 
        ( 1==m)?'Jan':( 2==m)?'Feb':(3==m)?'Mar':
        ( 4==m)?'Apr':( 5==m)?'May':(6==m)?'Jun':
        ( 7==m)?'Jul':( 8==m)?'Aug':(9==m)?'Sep':
        (10==m)?'Oct':(11==m)?'Nov':'Dec';
    
    	
      return "" +
        (d<10?"0"+d:d) + "-" +
        mmm + "-" +
        (y<10?"0"+y:y);
    }
    
    
    //
    // get last modified date of the 
    // current document.
    //
    function date_lastmodified()
    {
      var lmd = document.lastModified;
      var s   = "Unknown";
      var d1;
     
    
      // check if we have a valid date
      // before proceeding
      if(0 != (d1=Date.parse(lmd)))
      {
        s = "" + date_ddmmmyy(new Date(d1));
      }
    
      return s;
    }
    
    //
    // finally display the last modified date
    // as DD-MMM-YY
    //
    document.write( 
      "This page was updated on " + 
      date_lastmodified() );
    
    // -->
    </script>
    Last edited by VIPStephan; 11-12-2012 at 10:23 AM. Reason: corrected code BB tags

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,730
    Thanks
    202
    Thanked 2,507 Times in 2,485 Posts
    Once again your code is very much out of date! 2000 passed by some years ago and Netscape is one with Nineveh and Tyre.
    <script language=javascript> is long deprecated. Use <script type = "text/javascript"> instead (in fact also deprecated but still necessary for IE<9).
    The <!-- and //--> comment (hiding) tags have not been necessary since IE3 (i.e. since September 1997). If you see these in some published script it is a warning that you are looking at ancient and perhaps unreliable code.

    One more time - When posting here please help us to help you by following the posting guidelines and wrapping your code in CODE tags. It should be very obvious why this is necessary. This means use the octothorpe or # button on the toolbar. You can (and should) edit your previous post. ln fact the fault is the missing backslash in the closing tag. Review your posts and edit them where necessary.


    Code:
    <span id = "DLM"></span>
    
    <script type="text/javascript">
    
    function lastModified() {
    var mth = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
    var dy = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'];
    var modiDate = new Date(document.lastModified);
    var dd = modiDate.getDate();
    if (dd <10) { dd = "0" + dd }
    var m = modiDate.getMonth();  // 0-11
    m = mth[m];
    var yy = modiDate.getFullYear();
    var d = modiDate.getDay();  // 0-7
    d = dy[d];
    var lmd = d + "  " + dd + "-" + m + "-" + yy;
    var hh = modiDate.getHours();
    if (hh < 10) { hh = "0" + hh }
    var mm = modiDate.getMinutes();
    if (mm <10) { mm = "0" + mm}
    var ss = modiDate.getSeconds();
    if (ss <10) {ss = "0" + ss}
    var lmtm = hh + ":" + mm + ":" + ss;
    document.getElementById("DLM").innerHTML =  "This page was last updated on " + lmd + " @ " + lmtm;
    
    }
    
    //USAGE:-
    lastModified();
    </script>
    You can obviously edit this to output the date in any format that may be desired.

    Frustration - the first time you can't do it the second time.
    Desperation - the second time you can't do it the first time
    Last edited by Philip M; 11-11-2012 at 10:49 AM. Reason: typo

    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:

    ElizaKaye (11-11-2012)

  • #3
    New Coder
    Join Date
    Nov 2012
    Posts
    12
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Thank you very much for your code and suggestions. I am trying to teach myself from the internet mainly as my course is by correspondence and we don't get much help from the teachers.

    I ran the code above and this is what I received in the program.



    This page was last updated on 1 12-10-2012 @ 06:47:08.

    1. I don't know where the 1 is coming from.
    2. I need to get a day like (Monday, Tuesday) coming up according to the last modifed date.
    3. I don't need the time.
    4. It is a month late as I updated it today (Last modified: Monday, 12 November, 1012). This is the format I am trying to achieve.

  • #4
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,455
    Thanks
    0
    Thanked 632 Times in 622 Posts
    The only reason I can see why you'd be getting 1 instead of Monday is if you left out the line

    Code:
    d = dy[d];
    which is what converts the number representing the day of the week into the name of that day.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,730
    Thanks
    202
    Thanked 2,507 Times in 2,485 Posts
    If you don't need the time I have a clever suggestion - leave those lines out!!!
    If the month is wrong you have messed with the script and forgotton that in Javascript months are 0-11. Just use the script as I provided it.

    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.

  • #6
    New Coder
    Join Date
    Nov 2012
    Posts
    12
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Thank you very much.

    I found out the problem. When I copied it, it went into one line so I didn't bring down that line.

    How do I get it to display on the page?

  • #7
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,730
    Thanks
    202
    Thanked 2,507 Times in 2,485 Posts
    Quote Originally Posted by ElizaKaye View Post
    Thank you very much.

    I found out the problem. When I copied it, it went into one line so I didn't bring down that line.

    How do I get it to display on the page?
    Oh dear! You really ought to use the code you were given.

    Code:
    document.getElementById("DLM").innerHTML =  "This page was last updated on " + lmd + " @ " + lmtm;
    Are you having trouble with copying/pasting because the line breaks are lost (which is a recent forum problem I am trying to sort out with the mods?)
    Last edited by Philip M; 11-12-2012 at 07:59 AM.

    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.


  •  

    Posting Permissions

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