954,604 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Page Last Updated

On my site I use the following code to tell users when I last updated my website:

Last updated: ', new Date(document.lastModified).toLocaleString(), '


This shows users something like this:Last updated: 22 April 2006 12:53:12

Would it be possible to remove the time, so it just says:Last updated: 22 April 2006

ThanksMartin

martinkorner
Junior Poster
102 posts since Aug 2005
Reputation Points: 10
Solved Threads: 0
 

OOps - Sorry I missed part of my code out (thought it didn't make sense!)

Here is the real code I currently use:

<script type="text/javascript">
document.write ('Last updated: ', new Date(document.lastModified).toLocaleString())
</script>


But I would like to remove he time from it (as I said above)

Thanks AgainMartin

martinkorner
Junior Poster
102 posts since Aug 2005
Reputation Points: 10
Solved Threads: 0
 

You would use the various methods and properties of the Date() object to return just the portions you need:

http://www.w3schools.com/jsref/jsref_obj_date.asp

Or, you can use JavaScript's String() ojbect or RegEx to modify the string you display.

tgreer
Made Her Cry
Team Colleague
2,118 posts since Dec 2004
Reputation Points: 227
Solved Threads: 37
 

So would I do something like this?

<script type="text/javascript">
document.write ('Last updated: ', getDate(document.lastModified).toLocaleString(), 
getMonth(document.lastModified).toLocaleString(), 
getFullYear(document.lastModified).toLocaleString())
</script>


Hope that's right - I've not tested it though, so it's probably completely wrong!

ThanksMartin

martinkorner
Junior Poster
102 posts since Aug 2005
Reputation Points: 10
Solved Threads: 0
 

OK - that doesn't work because 'getDate' is undefined

Help?Martin

martinkorner
Junior Poster
102 posts since Aug 2005
Reputation Points: 10
Solved Threads: 0
 

Try this:

DLM=new Date(document.lastModified);
DLM_Y=DLM.getYear();
DLM_M=DLM.getMonth(); // Jan-Dec = 0-11
DLM_D=DLM.getDate();
if (DLM_Y < 70)   { DLM_Y=DLM_Y*1+2000; }
if (DLM_Y < 1000) { DLM_Y=DLM_Y*1+1900; }
DLM_M=DLM_M*1+1; // Jan-Dec = 1-12
if (DLM_M*1 < 10) { DLM_M='0'+DLM_M; } // leading zero
if (DLM_D*1 < 10) { DLM_D='0'+DLM_D; }
Out=DLM_M+'-'+DLM_D+'-'+DLM_Y;
document.write(Out);
jalarie
Light Poster
40 posts since Mar 2006
Reputation Points: 10
Solved Threads: 5
 

That gives me something like:
04-24-2006

But I would still prefer it without the dashes and in english order (dd-mm-yyyy) instead of American order (mm-dd-yyyy).
That's simple enough to change:DLM=new Date(document.lastModified);
DLM_Y=DLM.getYear();
DLM_M=DLM.getMonth(); // Jan-Dec = 0-11
DLM_D=DLM.getDate();
if (DLM_Y < 70) { DLM_Y=DLM_Y*1+2000; }
if (DLM_Y < 1000) { DLM_Y=DLM_Y*1+1900; }
DLM_M=DLM_M*1+1; // Jan-Dec = 1-12
if (DLM_M*1 < 10) { DLM_M='0'+DLM_M; } // leading zero
if (DLM_D*1 < 10) { DLM_D='0'+DLM_D; }
Out=DLM_D+' '+DLM_M+' '+DLM_Y;
document.write(Out);

But I would also like the month to be in text format:24 April 2006

Can anyone canvert 1 into January, 2 to February etc.?Martin

martinkorner
Junior Poster
102 posts since Aug 2005
Reputation Points: 10
Solved Threads: 0
 

All of the information you need is in the link I provided above. With all due respect, I encourage you to do some of this work yourself. That's why I pointed you to the reference, rather than writing the code for you.

If you click that link, you'll see a complete list of every date-related Javascript method. Clicking "getMonth()" shows you detailed documenation about that method, including sample code for converting the month-number to a month name.

tgreer
Made Her Cry
Team Colleague
2,118 posts since Dec 2004
Reputation Points: 227
Solved Threads: 37
 

OK - sorry, I just assumed that it only explained what was said in the phrase by the link Returns the month from a Date object (from 0-11)

Really Sorry ;)Martin

martinkorner
Junior Poster
102 posts since Aug 2005
Reputation Points: 10
Solved Threads: 0
 

I'm not mad, so no apology necessary. I just know from experience that you learn better by figuring things out yourself. This forum is a great resource for pointers in the right direction, so there was nothing wrong with starting the thread and asking some questions. You have to follow-up on the answers, though!

I hope your project turns out well.

tgreer
Made Her Cry
Team Colleague
2,118 posts since Dec 2004
Reputation Points: 227
Solved Threads: 37
 

Alright, thanks:cheesy: !

I'm working at it...

Martin

martinkorner
Junior Poster
102 posts since Aug 2005
Reputation Points: 10
Solved Threads: 0
 
http://www.w3schools.com/jsref/jsref_obj_date.asp


Please be aware that the "getDay" function always returns 0-6, but sometimes means Sunday-Saturday and sometimes means Monday-Sunday depending on how you have localized your machine.

jalarie
Light Poster
40 posts since Mar 2006
Reputation Points: 10
Solved Threads: 5
 

It's OK, I'm not putting the day on it.

Thanks anyway.:cheesy:

Martin

martinkorner
Junior Poster
102 posts since Aug 2005
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You