Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Show date web page altered 4

Status
Not open for further replies.

Sregor

Technical User
Mar 24, 2002
15
0
0
GB
I am currently using the following code to automatically indicate when the web page was last updated:

<script language=&quot;javascript&quot;>
document.write(&quot;The page was last modified on&quot;+&quot;&nbsp;&quot;+document.lastModified)
</script>

The problem I have is 'cometic', the date shows as MM/DD/YYYY HH:MM:SS

How do I force to read DD/MM/YYYY HH:MM:SS?

The website is
Sandy Rogers.
Sandy@ALSxUK.freeserve.co.uk
 
<script language=&quot;javascript&quot;>
var mydate = new Date(document.lastModified);
var day = mydate.getDay();
var month = mydate.getMonth();
var year = mydate.getYear();
var seconds = mydate.getSeconds();
var minutes = mydate.getMinutes();
var hours = mydate.getHours();
document.write(&quot;The page was last modified on &quot; + day + &quot;/&quot; + month + &quot;/&quot; + year + &quot; &quot; + hours + &quot;:&quot; + minutes + &quot;:&quot; + seconds);
</script>
....:::::.... xxLargeWASP ....:::::....
 
sorry to improve you xxLargeWASP , but this are my points:

var day = mydate.getDay(); gives the Day of the week. It returns values between 0(sunday) and 6(saturday)
I think you should use var day = mydate.getDate();

var day = mydate.getMonth(); returns values between 0(January) and 11(December) So you should use var day = mydate.getMonth()+1;

I think Sregor wants the Fullyear YYYY. But for some how Javascript returns 1902 for the Fullyear in the combination &quot;new Date(document.lastModified)&quot;

so I should suggest var year = mydate.getFullYear()+100;

Here is the total code:

var mydate = new Date(document.lastModified);
var day = mydate.getDate();
var month = mydate.getMonth()+1;
var year = mydate.getFullYear()+100;
var seconds = mydate.getSeconds();
var minutes = mydate.getMinutes();
var hours = mydate.getHours();
document.write(&quot;The page was last modified on &quot; + day + &quot;/&quot; + month + &quot;/&quot; + year + &quot; &quot; + hours + &quot;:&quot; + minutes + &quot;:&quot; + seconds);

Hope this helps,
Erik <!-- My sport: Boomerang throwing !!
This year I will participate at the World Championships in Germany. (!! Many Happy Returns !! -->
 
by the way, I'm not sure if document.lastModified is what you realy want? In my tests the time change with every &quot;refresh&quot;.
Can somebody (xutopia ??)tell me why??? My reference tells me nothing about it!

Sregor, maybe it's enough for you to only indicate the Date and not the Time. In that case maybe the &quot;fileModifiedDate&quot; property is a good alternative?

It gives you immediately the &quot;Long Date&quot; of the user's &quot;Reginonal settings&quot;, so you don't have to convert the date at all.

<script language=&quot;javascript&quot;>
document.write(&quot;The page was last modified on&quot;+&quot; &quot;+document.fileModifiedDate)
</script>

Hope this helps,
Erik

p.s.:
For those how want it to know:
There is also the property document.fileUpdatedDate&quot; . But in my tests this gives a STRANGE date. So DO NOT USE IT. I don't know why this is? Maybe because it's an obsolete property perhaps??? Who can tell me this ???

Erik <!-- My sport: Boomerang throwing !!
This year I will participate at the World Championships in Germany. (!! Many Happy Returns !! -->
 
Thankyou for the quick replies.

I was trying to avoid lots of javascript and only wanted to display piece of text that would reflect the last time the web page had been modified (not accessed).

Boomerang your last piece of code:

<script language=&quot;javascript&quot;>
document.write(&quot;The page was last modified on&quot;+&quot; &quot;+document.fileModifiedDate)
</script>

showed the following display:
The page was last modified on 05/06/2002.
To anyone in the UK they woould automatically assume the date was 5th June 2002 instead of 6th May 2002

The following code:
<script>
var mydate = new Date(document.lastModified);
var day = mydate.getDate();
var month = mydate.getMonth()+1;
var year = mydate.getFullYear()+100;
var seconds = mydate.getSeconds();
var minutes = mydate.getMinutes();
var hours = mydate.getHours();
document.write(&quot;The page was last modified on &quot; + day + &quot;/&quot; + month + &quot;/&quot; + year + &quot; &quot; + hours + &quot;:&quot; + minutes + &quot;:&quot; + seconds);
</script>

Displayed The page was last modified on 6/5/2102 15:59:3

I use some code that is similar on my index page I think I am going to have to play with that. It is as follows:

var dayarray=new Array(&quot;Sunday&quot;,&quot;Monday&quot;,&quot;Tuesday&quot;,&quot;Wednesday&quot;,&quot;Thursday&quot;,&quot;Friday&quot;,&quot;Saturday&quot;)
var montharray=new Array(&quot;January&quot;,&quot;February&quot;,&quot;March&quot;,&quot;April&quot;,&quot;May&quot;,&quot;June&quot;,&quot;July&quot;,&quot;August&quot;,&quot;September&quot;,&quot;October&quot;,&quot;November&quot;,&quot;December&quot;)
function getthedate(){
var mydate=new Date()
var year=mydate.getYear()
if (year < 1000) year+=1900
var day=mydate.getDay()
var month=mydate.getMonth()
var daym=mydate.getDate()
var hours=mydate.getHours()
var minutes=mydate.getMinutes()
var seconds=mydate.getSeconds()
// var dn=&quot;am&quot;
// if (hours>=12) dn=&quot;pm&quot;
// The greeting coding is as follows
var greeting=&quot;Hello&quot; // default greeting
// Original code if (hours>=0 && hours<12) greeting=&quot;morning&quot;+&quot;<BR>&quot;
if (hours>=0 && hours<12) greeting=&quot;morning&quot;
if (hours>=12 && hours<18) greeting=&quot;afternoon&quot;
if (hours>=18 && hours<=23) greeting=&quot;evening&quot; // greeting coding finished
if (hours>12){hours=hours-12}
if (hours==0) hours=&quot;00&quot;
if (minutes<10) minutes=&quot;0&quot;+minutes
if (seconds<10) seconds=&quot;0&quot;+seconds
var suffix=&quot;&quot; // suffix code ie st, nd, rd and th
if ((daym ==1) | (daym ==21) | (daym ==31)){suffix = &quot;st &quot;}
if ((daym ==2) | (daym ==22)){suffix = &quot;nd &quot;}
if ((daym ==3) | (daym ==23)){suffix = &quot;rd &quot;}
if((daym >=4 && daym <=20) | (daym >=24 && daym <=30)){suffix = &quot;th &quot;}
var cdate=&quot;<font color='FF0000' size='2pt'>&quot;+&quot;Your system time is &quot;+hours+&quot;:&quot;+minutes+&quot;:&quot;+seconds+&quot; on &quot;+dayarray[day]+&quot; &quot;+greeting+&quot; the &quot;+daym+&quot;<font size=-1><sup>&quot;+suffix+&quot;</sup></font>&quot;+&quot; of &quot;+montharray[month]+&quot; &quot;+year+&quot;</font></font>&quot;
// Original var cdate=&quot;<font color='FF0000'>&quot;+greeting+&quot;&nbsp;&nbsp;Your system time is &quot;+hours+&quot;:&quot;+minutes+&quot;:&quot;+seconds+dn+&quot; - &quot;+dayarray[day]+&quot;, &quot;+daym+&quot;<font size=-1><sup>&quot;+suffix+&quot;</sup></font>&quot;+&quot; &quot;+montharray[month]+&quot; &quot;+year+&quot;</font></font>&quot;
// fonts are done as follows var cdate=&quot;<small><font color='000066' face='Arial'><b>&quot;+hours+&quot;:&quot;+minutes+&quot;:&quot;+seconds+dn+&quot; - &quot;+dayarray[day]+&quot;, &quot;+daym+&quot; &quot;+montharray[month]+&quot; &quot;+year+&quot;</b></font></small>&quot;
if (document.all) document.all.clock.innerHTML=cdate
else if (document.getElementById) document.getElementById(&quot;clock&quot;).innerHTML=cdate
else document.write(cdate)}
if (!document.all&&!document.getElementById) getthedate()
function goforit(){if (document.all||document.getElementById) setInterval(&quot;getthedate()&quot;,1000)}

and displays:

Your system time is 4:05:22 on Monday afternoon the 6th of May 2002.

Again, Thanks for your quick replies and I now have something to work from.

Sandy Rogers

 
Mozilla seems to only support document.lastModified and IE supports it as well since version 3.something. If the mozilla group decided for this one there is a reason :)

The question by sregor was :

The problem I have is 'cometic', the date shows as MM/DD/YYYY HH:MM:SS

How do I force to read DD/MM/YYYY HH:MM:SS?


my recommended solution is as follows :

<script>
function lastModified()
{
if (!document.lastModified)
{
return;// not capable of using document.lastModified
}

var datePieces = document.lastModified.split(&quot;/&quot;)
document.write(datePieces[1] + &quot;/&quot; + datePieces[0] + &quot;/&quot; + datePieces[2])
}

lastModified()
</script>

Hope this helps.

PS: boomerang, maybe the reason why it didn't work for you had to do with server problems or something or other. It seemed to work fine every time I used it. Gary Haran
 
Xutopia,

Thanks for the code, I will 'upload' it to my pages on in the next 24 hours.

Thanks again to all who replied, it's nice to know there's help out there when you need it.

Regards.
Sandy Rogers
 
Another Star for xutopia. Sometimes we read far more into the question than is really asked. That can either be good or bad - depending on the situation.

Simple, elegant solutions that answer the original question are always the best in my book.

Way to go, xutopia! [yoda] <= picture of xutopia. Einstein47
(Love is like PI - natural, irrational, endless, and very important.)
 
Be careful folks, when you're writing code that expects this value to be in a specific format. document.lastModified returns a string exactly as it was provided by the server. There's no standard that says what order you're going to get those values in, or even that you're going to get those values as numbers. It's dependent on OS, web server implementation, and local settings on the server.

Just a note of caution!
 
Okay, where does that leave me now that I have updated my web pages with the code provided?

And how did Einstein47 give a star to Xutopia?

Sandy Rogers
Sandy@ALSxUK.freeserve.co.uk
 
Forget the 'star' bit on my last post I was obviously typing with my brain in nuetral. Duh!!

Sandy Rogers
Sandy@ALSxUK.freeserve.co.uk
 
Here's a good page accounting for time zone offset and such here:

I guess my advice is, your best bet is to convert the document.lastModified string into a JS Date object, and use the localization features built into JS to account for differences in date format, timezone, etc. If you just count on the string being in a specific format, you may be disappointed.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top