Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Show Date and Time on a web Page

Status
Not open for further replies.
Jan 29, 2002
30
0
0
GB
Hi there, I am looking to show the date and time on a web page. I found a javascript on javascript.com and this does pretty much everything but i can not change the font size etc of the date section. I was hoping someone would be able to help.

Cheers
James

Here is the script.


<script language=&quot;JavaScript&quot;>
<!-- This figures out what day of the week it is, and the date, and year. -->
<!--
s_date = new Date();
var weekDay = &quot;&quot;;

selectMonth = new Array(12);
selectMonth[0] = &quot;January&quot;;
selectMonth[1] = &quot;February&quot;;
selectMonth[2] = &quot;March&quot;;
selectMonth[3] = &quot;April&quot;;
selectMonth[4] = &quot;May&quot;;
selectMonth[5] = &quot;June&quot;;
selectMonth[6] = &quot;July&quot;;
selectMonth[7] = &quot;August&quot;;
selectMonth[8] = &quot;September&quot;;
selectMonth[9] = &quot;October&quot;;
selectMonth[10] = &quot;November&quot;;
selectMonth[11] = &quot;December&quot;;

if(s_date.getDay() == 1){
weekDay = &quot;Monday&quot;;
}
if(s_date.getDay() == 2){
weekDay = &quot;Tuesday&quot;;
}
if(s_date.getDay() == 3){
weekDay = &quot;Wednesday&quot;;
}
if(s_date.getDay() == 4){
weekDay = &quot;Thursday&quot;;
}
if(s_date.getDay() == 5){
weekDay = &quot;Friday&quot;;
}
if(s_date.getDay() == 6){

weekDay = &quot;Saturday&quot;;
}
if(s_date.getDay() == 7){
weekDay = &quot;Sunday&quot;;
}
if(s_date.getDay() == 0){
weekDay = &quot;Sunday&quot;;
}


var setYear = s_date.getYear();

var BName = navigator.appName;

if(BName == &quot;Netscape&quot;){
var setYear = s_date.getYear() + 1900;
}

document.write(weekDay + &quot;, &quot; + selectMonth[s_date.getMonth()] + &quot; &quot; +
s_date.getDate() + &quot;, &quot; + setYear);

// -->

</script>

<div id=Clock align=Center style=&quot;font-family Tahoma; font-size: 28; color:Black&quot;>&nbsp;</div>

<script language = &quot;JavaScript&quot;>

//This script was created by Gary Perry
//Email: shadow_chaser@hotmail.com
//Web:
//This script will display a real time clock at the top of your webpage.


//Put this code in the body section of your webpage, and that's all
//there is to it. No images to worry about, nothing, except it
//only works for IE 4 and above.

//Change the color of the font above in the div tag to match your webpage
//colors.


function tick() {
var hours, minutes, seconds, ap;
var intHours, intMinutes, intSeconds;
var today;

today = new Date();

intHours = today.getHours();
intMinutes = today.getMinutes();
intSeconds = today.getSeconds();

switch(intHours){
case 0:
intHours = 12;
hours = intHours+&quot;:&quot;;
ap = &quot;A.M.&quot;;
break;
case 12:
hours = intHours+&quot;:&quot;;
ap = &quot;P.M.&quot;;
break;
case 24:
intHours = 12;
hours = intHours + &quot;:&quot;;
ap = &quot;A.M.&quot;;
break;
default:
if (intHours > 12)
{
intHours = intHours - 12;
hours = intHours + &quot;:&quot;;
ap = &quot;P.M.&quot;;
break;
}
if(intHours < 12)
{
hours = intHours + &quot;:&quot;;
ap = &quot;A.M.&quot;;
}
}


if (intMinutes < 10) {
minutes = &quot;0&quot;+intMinutes+&quot;:&quot;;
} else {
minutes = intMinutes+&quot;:&quot;;
}

if (intSeconds < 10) {
seconds = &quot;0&quot;+intSeconds+&quot; &quot;;
} else {
seconds = intSeconds+&quot; &quot;;
}

timeString = hours+minutes+seconds+ap;

Clock.innerHTML = timeString;

window.setTimeout(&quot;tick();&quot;, 100);
}

window.onload = tick;

</script>
 
move the closing </div> tag down from right after the <div> to after your closing </script> tag. The way it is right now, the div isn't doing anything. Next, you probably have to fix the syntax in the style attribute of the <div> tag
<div id=Clock align=Center style=&quot;font-family: Tahoma; font-size: 28; color:Black&quot;>
you were missing a :, that should do it for you. I didn't check the script or anyhting, but you said that works, so...

I hope this helps


dis
 
Thanks for that. I solved it with what i think is a better script. Less going on i think. So i am posting it for all to see.

Cheers
James

<BODY>

<body onLoad=&quot;goforit()&quot;>


<script language=&quot;JavaScript&quot;>



var dayarray=new Array(&quot;Sunday&quot;,&quot;Monday&quot;,&quot;Tuesday&quot;,&quot;Wednesday&quot;,&quot;Thursday&quot;,&quot;Friday&quot;,&quot;Saturday&quot;)
var montharray=new Array(&quot;January&quot;,&quot;February&quot;,&quot;March&quot;,&quot;April&quot;,&quot;May&quot;,&quot;June&quot;,&quot;July&quot;,&quot;August&quot;,&quot;September&quot;,&quot;October&quot;,&quot;November&quot;,&quot;December&quot;)

function getthedate(){
var mydate=new Date()
var year=mydate.getYear()
if (year < 1000)
year+=1900
var day=mydate.getDay()
var month=mydate.getMonth()
var daym=mydate.getDate()
if (daym<10)
daym=&quot;0&quot;+daym
var hours=mydate.getHours()
var minutes=mydate.getMinutes()
var seconds=mydate.getSeconds()
var dn=&quot;AM&quot;
if (hours>=12)
dn=&quot;PM&quot;
if (hours>12){
hours=hours-12
}
if (hours==0)
hours=12
if (minutes<=9)
minutes=&quot;0&quot;+minutes
if (seconds<=9)
seconds=&quot;0&quot;+seconds
//change font size here
var cdate=&quot;<center><font color='000000'; font size='1pt'; face='Arial'><b>&quot;+dayarray[day]+&quot;, &quot;+montharray[month]+&quot; &quot;+daym+&quot;, &quot;+year+&quot; &quot;+hours+&quot;:&quot;+minutes+&quot;:&quot;+seconds+&quot; &quot;+dn
+&quot;</b></font></center>&quot;
if (document.all)
document.all.clock.innerHTML=cdate
else
document.write(cdate)
}
if (!document.all)
getthedate()
function goforit(){
if (document.all)
setInterval(&quot;getthedate()&quot;,1000)
}

</script>
<span id=&quot;clock&quot;></span>
 
Rather than use getYear(), use getFullYear() and you won't have to add 1900 to the year.

Also, combine:

if (hours>=12)
dn=&quot;PM&quot;
if (hours>12){
hours=hours-12
}

to:

if (hours>=12)
{
dn=&quot;PM&quot;;
if (hours > 12) hours-=12;
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top