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

Thanks
Martin

Dani AI

Generated

Short practical answer: use the Date API to extract just day, month name and year (and avoid getYear()/getDay() pitfalls). was right to point you at the Date methods, and ’s example shows the right idea — but prefer getFullYear() and, in modern pages, avoid document.write. Also note: document.lastModified can be unreliable for dynamically generated pages or if the server does not set a proper Last-Modified; for guaranteed accuracy set the date server-side or during your build.

A clean client-side approach that produces "24 April 2006" and does not use document.write:

<span id="lastUpdated"></span>

<script>
var d = new Date(document.lastModified);
var opts = { day: 'numeric', month: 'long', year: 'numeric' };
document.getElementById('lastUpdated').textContent =
  'Last updated: ' + d.toLocaleDateString('en-GB', opts);
</script>

If you prefer an explicit month-name mapping (works in very old browsers):

var d = new Date(document.lastModified);
var months = ['January','February','March','April','May','June','July','August','September','October','November','December'];
var formatted = d.getDate() + ' ' + months[d.getMonth()] + ' ' + d.getFullYear();
document.getElementById('lastUpdated').textContent = 'Last updated: ' + formatted;

Server-side is the most reliable option (example in PHP):

<?php echo 'Last updated: ' . date('j F Y', filemtime(__FILE__)); ?>

Troubleshooting notes: test on the target server (dynamic pages often show the current date), avoid getDay() when you mean day-of-month, and prefer getFullYear() over getYear() to avoid year-calc hacks.

Recommended Answers

All 12 Replies

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 Again
Martin

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.

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!

Thanks
Martin

OK - that doesn't work because

'getDate' is undefined

Help?

Martin

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

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

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.

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

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.

Alright, thanks:cheesy: !

I'm working at it...

Martin

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.

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

Thanks anyway.:cheesy:

Martin

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.