View Full Version : Local Time Script - Time Format

12-31-2008, 03:43 AM
1) Script Title: Local Time Script

2) Script URL (on DD): http://www.dynamicdrive.com/dynamicindex6/localtime.htm

3) Describe problem: I just first want to say how much I love this site. There are a MILLION free script/tutorial sites online but this is by far my favorite. The beauty of your site is that EVERYTHING actually works. I have yet to find a script that did not work here and that is very rare online!!!! Great work =)

Okay, now my questions... is there any way I can change the time format for the Local Time Script? The site I'm working on is for a company in Quebec so... instead of 1:00 PM, it would be 13:00 // 10:00 PM would be 22:00 etc...

Any chance I get get some help making this format change? Thanks in advance.

12-31-2008, 05:22 AM
Correct me if I'm wrong. It looks like you are happy with the script, but would like the time displayed in 24 hour format, and sans AM/PM, right?

If so, and this would apply to the only time that you wish displayed (I'm guessing here that you want only 24 hour format Quebec time), then the following modifications should suffice:

var thisobj=this
if (this.displayversion=="long")
var hour=this.localtime.getHours()
var minutes=this.localtime.getMinutes()
var seconds=this.localtime.getSeconds()
var ampm=""
var dayofweek=weekdaystxt[this.localtime.getDay()]
this.container.innerHTML=formatField(hour)+":"+formatField(minutes)+":"+formatField(seconds)+" "+ampm+" ("+dayofweek+")"
setTimeout(function(){thisobj.updateContainer()}, 1000) //update container every second

Untested, but should work out as I stipulate. If you want the flexibility to show multiple times in the same 24 hour format, this should still be fine. However if you also want to be able to display some times in 12 hour AM/PM format, more modification would be required.

12-31-2008, 05:31 PM
This worked PERFECTLY!!!! Thank you so much John, I really appreciate you taking the time to help me with this.

Oh and... Happy New Year!! :)

05-24-2011, 04:03 PM
Thnaks for that, I've managed to get the 24 hr format working and removed the AM/PM but can you tell me how I can remove the seconds?

I basically want to to show HH:MM, is this possible?

05-24-2011, 04:39 PM
Just get rid of that part. Let's zoom in on the long highlighted line from my last post:

this.container.innerHTML=formatField(hour)+":"+formatField(minutes)+":"+formatField(seconds)+" "+ampm+" ("+dayofweek+")"

To get rid of the seconds, remove the highlighted. To have only HH:MM make that line like so:


05-25-2011, 03:56 AM
Hi John,

Thanks for the quick response and solution!

I did notice however that what should be 06:00 is displayed as 6:00 and this could be confusing if international visitors are not aware of the format. Is there any way to fix this issue?

Thanks again

05-25-2011, 05:35 AM
Shouldn't do that. The format field function should add the 0 prefix. Be sure to use:


as in my previous post, NOT (as in the original script):

formatField(hour, 1)

If you want more help:

Please post a link to a page on your site that contains the problematic code so we can check it out.

05-25-2011, 03:03 PM
Thanks again John,

I'll have to until the morning to see if it's worked ;)

06-20-2011, 12:37 PM
Worked perfectly with your instructions.

Many Thanks