0

Hello everybody,
First off let me say I did do a search of google and these forums before posting this. I have sort of what I want without having exactly what I want and before I proceed further hacking up already working code I figured I'd start a conversation as to whether my goal is at all possible.

Currently using (thank you w3schools as I am by no means a javascript expert):

document.writeln(new Date())

I get the following: Wed Jul 13 2011 09:24:09 GMT-0600 (Mountain Daylight Time)
which would be great for most people, but I'm kind of picky so I want the clock to keep ticking and to show the current time.

To do this I use this code(again thank you w3schools):

function startTime()
{
var today=new Date();
var h=today.getHours();
var m=today.getMinutes();
var s=today.getSeconds();
// add a zero in front of numbers<10
m=checkTime(m);
s=checkTime(s);
document.getElementById('txt').innerHTML=h+":"+m+":"+s;
t=setTimeout('startTime()',500);
}

function checkTime(i)
{
if (i<10)
  {
  i="0" + i;
  }
return i;
}

This displays: 09:24:09.

Now for the question is it possible to hack up the

document.writeln(new Date())

to display as follows:
09:24:09 (Mountain Daylight Time)

with the time being that of my clock? Or am I going to be forced to learn javascript and create my own script that gets the time, checks the timezone and converts it to MST for example?

Also while we are on the topic of formatting can that Mountain Daylight Time be formatted to be MDT/MST?

2
Contributors
4
Replies
5
Views
6 Years
Discussion Span
Last Post by PixelatedKarma
1

This is the fastest and shortest I can do for you:
(The MDT/MST will depend on your clock settings. If you've chosen MST, MST display it will!)

<!DOCTYPE native>
<html>
<head>
<title>Live Clock</title>
   <style>
	div
	{
		background-color: silver;
		border: 1px solid gray;
		padding: 1px 5px;
		border-radius: 4px;
	}
   </style>
</head>
<body>
<div>
	Time: &nbsp; <input id="time">
</div>
<script>
onload=
	liveClock=
		function(){
		var tm = Date();
			tm=(tm.toString()).split(' ');
			time.value = tm[3]+" "+tm[4];
		setTimeout("liveClock()", 500);
	}
</script>
</body>
</html>
0

Thank you Troy and sorry for taking so long to post back, what I ended up doing was this:

echo '<script type="text/javascript">
						function startTime()
							{
							var today=new Date();
							var h=today.getHours();
							var m=today.getMinutes();
							var s=today.getSeconds();
							// add a zero in front of numbers<10
							m=checkTime(m);
							s=checkTime(s);
							document.getElementById("time").innerHTML=h+":"+m+":"+s;
							t=setTimeout("startTime()",500);
							}

							function checkTime(i)
							{
							if (i<10)
								{
								i="0" + i;
								}
							return i;
						}
						</script>';
						
						echo'<div id="time">&nbsp;</div>';

Which outputs: Hour:Minute:Second

Followed by:

echo'<div id="tz">[
						<script type="text/javascript">
							var rightNow = new Date();
							document.write(String(String(rightNow).split("(")[1]).split(")")[0]);
						</script>&nbsp;]
					</div>';

Which outputs the timezone inbetween [ ]. Only problem is it doesn't always show the timezone depending on if the person is hiding behind a proxy. Your way seems alot simpler but as I explained in a PM I was a bit tied up in real life.

0

Ya I'm no javascript expert but it works. And it might look a bit weird cause I have it embedded in a php page.

Edited by PixelatedKarma: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.