6 replies [Last post]
Alan
Offline
Enthusiast
Last seen: 17 years 6 weeks ago
Joined: 2003-10-20
Posts: 72
Points: 0

Hi,

I use the following code to display the "Last Saved" or should I say "Page Updated" date on my webpages. The code displays the date in "10/08/2004" format.

// Javascript @ Beert.be
// http://www.beert.be/javascript/

var date = new Date(document.lastModified);
var day = date.getDate();
var month = [date.getMonth() + 1];
var year = date.getYear();
if (day < 10) day = "0" + day;
if (month < 10) month = "0" + month;
if (year < 2000) year += 1900;
document.write(day + "/" + month + "/" + year);

What I'd like to do is modify this code so that it displays the date in "10 August 2004" format. I can see that the month variable simply needs changing to be assigned a name, but I don't know how to program this. Can anyone help?

Tags:
Stu
Stu's picture
Offline
Enthusiast
Bristol uk
Last seen: 16 years 45 weeks ago
Bristol uk
Joined: 2004-01-20
Posts: 282
Points: 0

Javascript Date Question

<!-- Begin
var days = new Array(8);
days[1] = "Sunday";
days[2] = "Monday";
days[3] = "Tuesday";
days[4] = "Wednesday";
days[5] = "Thursday";
days[6] = "Friday";
days[7] = "Saturday";
var months = new Array(13);
months[1] = "January";
months[2] = "February";
months[3] = "March";
months[4] = "April";
months[5] = "May";
months[6] = "June";
months[7] = "July";
months[8] = "August";
months[9] = "September";
months[10] = "October";
months[11] = "November";
months[12] = "December";
var dateObj = new Date(document.lastModified)
var wday = days[dateObj.getDay() + 1]
var lmonth = months[dateObj.getMonth() + 1]
var date = dateObj.getDate()
var fyear = dateObj.getYear()
if (fyear < 2000)
fyear = fyear + 1900
document.write(wday + ", " + lmonth + " " + date + ", " + fyear)
// End -->

This will sort out the day and month

It's not what you do it's the way that you do it.
So do it with STYLE
http://www.s7u.co.uk

Alan
Offline
Enthusiast
Last seen: 17 years 6 weeks ago
Joined: 2003-10-20
Posts: 72
Points: 0

Javascript Date Question

Hi Stu,

Thanks for that solution. The only problem is, I think you over-engineered it a little. Using that code, the last saved date is displayed as "Friday, August 6, 2004". What I want it to do is display as "06 August 2004".

Could you please edit your code to make it do this? I really know nothing about JavaScript, so I would appreciate your help.

Alan
Offline
Enthusiast
Last seen: 17 years 6 weeks ago
Joined: 2003-10-20
Posts: 72
Points: 0

Javascript Date Question

Hi there,

So, can anyone help me with this one? Or is this perhaps not the best place to ask?

Stu
Stu's picture
Offline
Enthusiast
Bristol uk
Last seen: 16 years 45 weeks ago
Bristol uk
Joined: 2004-01-20
Posts: 282
Points: 0

Javascript Date Question

Just a minor tweak (which perhaps you may have attempted Smile )

<!-- Begin
var months = new Array(13);
months[1] = "January";
months[2] = "February";
months[3] = "March";
months[4] = "April";
months[5] = "May";
months[6] = "June";
months[7] = "July";
months[8] = "August";
months[9] = "September";
months[10] = "October";
months[11] = "November";
months[12] = "December";
var dateObj = new Date(document.lastModified)
var lmonth = months[dateObj.getMonth() + 1]
var date = dateObj.getDate()
if (date <10 )
date = "0" + date
var fyear = dateObj.getYear()
if (fyear < 2000)
fyear = fyear + 1900
document.write(date + " " + lmonth + " " + fyear)
// End -->

If this is a new web site then you can safely delete the two lines
if (fyear < 2000)
fyear = fyear + 1900
as these are put in for pages last updated pre 2000

It's not what you do it's the way that you do it.
So do it with STYLE
http://www.s7u.co.uk

Alan
Offline
Enthusiast
Last seen: 17 years 6 weeks ago
Joined: 2003-10-20
Posts: 72
Points: 0

Javascript Date Question

Bravo Stu,

That works perfectly. Thank you very much indeed. I'll have a closer look through the code later on, to try and understand it, even if I know nothing about JavaScript Smile

Alan

Stu
Stu's picture
Offline
Enthusiast
Bristol uk
Last seen: 16 years 45 weeks ago
Bristol uk
Joined: 2004-01-20
Posts: 282
Points: 0

Javascript Date Question

Sorry, small error in my last post.

If this is a new web site then you can safely delete the two lines
if (fyear < 2000)
fyear = fyear + 1900
as these are put in for pages last updated pre 2000

Should have been ~

You will NEED the two lines
if (fyear < 2000)
fyear = fyear + 1900

To display the year correctly in FF etc

It's not what you do it's the way that you do it.
So do it with STYLE
http://www.s7u.co.uk