View Full Version : trying to get day from date last modified

11-11-2012, 07:13 AM
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

// 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':

return "" +
(d<10?"0"+d:d) + "-" +
mmm + "-" +

// 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
"This page was updated on " +
date_lastmodified() );

// -->

Philip M
11-11-2012, 08:58 AM
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.

<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;



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

11-11-2012, 07:52 PM
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.

11-11-2012, 08:10 PM
The only reason I can see why you'd be getting 1 instead of Monday is if you left out the line

d = dy[d];

which is what converts the number representing the day of the week into the name of that day.

Philip M
11-11-2012, 08:51 PM
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.

11-12-2012, 07:09 AM
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?

Philip M
11-12-2012, 07:55 AM
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.

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?)