Forum Moderators: open

Message Too Old, No Replies

I just want to add a space!

Adding a space to my document.write()

         

Baxter30

5:34 am on Dec 19, 2008 (gmt 0)

10+ Year Member



I just got through writing a script that will display the date and a clock on my site but I am having trouble separating the clock and the AM/PM symbols. Heres the code for the insertion point.

<SCRIPT LANGUAGE="JavaScript">
<!--

// get date object
var today = new Date();
var hour = today.getHours();
var suf = " AM";

if (hour>12) {
var suf = " PM"
}

// write the date
document.write(today.toLocaleDateString() + ' <span id=\"clock\"></span> ' + suf);

-->
</SCRIPT>

I want to put a space between the clock and the suf variable but it won't let me for some reason. I'm totally stumped any help would be appreciated.

[edited by: tedster at 7:49 am (utc) on Dec. 19, 2008]

daveVk

11:08 am on Dec 19, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You need to find the javascript that updates the clock, it probable also changes the AM/PM display.

Look for other references to the word 'clock' in the javascript.

Baxter30

2:09 pm on Dec 19, 2008 (gmt 0)

10+ Year Member



The script for clock only creates the clock itself (hh:mm:ss) not the AM/PM but I think I can write a scrip in the clock script to do that with a space, thanks man.
The clock code as of now:

<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);
h=norm(h);
document.getElementById('txt').innerHTML=h+":"+m+":"+s;
t=setTimeout('startTime()',500);
}

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

function norm(i) {
if (i>12) { i=i - 12 }
return i;
}
</script>

I'll let you know if it works.

Baxter30

4:31 pm on Dec 19, 2008 (gmt 0)

10+ Year Member



Woot!
It worked beautifully. There is now a space between the last number in the clock and the AM or PM, but I'm still a little confused. Why didn't this work earlier when I placed the suf variable in the body.

Script now:

<html>
<head>
<script type="text/javascript">
function startTime()
{
var today=new Date();
var h=today.getHours();
var m=today.getMinutes();
var s=today.getSeconds();
var suf = " AM";

if (hour>12) {
var suf = " PM"
}
// add a zero in front of numbers<10
m=checkTime(m);
s=checkTime(s);
h=norm(h);
document.getElementById('txt').innerHTML=h+":"+m+":"+s+suf;
t=setTimeout('startTime()',500);
}

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

function norm(i) {
if (i>12) { i=i - 12 }
return i;
}
</script>
</head>

<body onLoad="startTime()">

<SCRIPT LANGUAGE="JavaScript">
<!--
// get date object
var today = new Date();

// write the date
document.write(today.toLocaleDateString() + ' <span id=\"txt\"></span> ');

-->
</SCRIPT>
</body>
</html>

The Fully Functional Clock