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-11-2012, 07:13 AM   PM User | #1
ElizaKaye
New Coder

 
Join Date: Nov 2012
Posts: 12
Thanks: 4
Thanked 0 Times in 0 Posts
ElizaKaye is an unknown quantity at this point
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
ElizaKaye is offline   Reply With Quote
Old 11-11-2012, 08:58 AM   PM User | #2
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
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
__________________

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.

Last edited by Philip M; 11-11-2012 at 10:49 AM.. Reason: typo
Philip M is offline   Reply With Quote
Users who have thanked Philip M for this post:
ElizaKaye (11-11-2012)
Old 11-11-2012, 07:52 PM   PM User | #3
ElizaKaye
New Coder

 
Join Date: Nov 2012
Posts: 12
Thanks: 4
Thanked 0 Times in 0 Posts
ElizaKaye is an unknown quantity at this point
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.
ElizaKaye is offline   Reply With Quote
Old 11-11-2012, 08:10 PM   PM User | #4
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,530
Thanks: 0
Thanked 503 Times in 494 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
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/
felgall is offline   Reply With Quote
Old 11-11-2012, 08:51 PM   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
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.
Philip M is offline   Reply With Quote
Old 11-12-2012, 07:09 AM   PM User | #6
ElizaKaye
New Coder

 
Join Date: Nov 2012
Posts: 12
Thanks: 4
Thanked 0 Times in 0 Posts
ElizaKaye is an unknown quantity at this point
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?
ElizaKaye is offline   Reply With Quote
Old 11-12-2012, 07:55 AM   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 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?)
__________________

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.

Last edited by Philip M; 11-12-2012 at 07:59 AM..
Philip M 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 06:52 PM.


Advertisement
Log in to turn off these ads.