...

View Full Version : crossbrowser date and time



tpeck
08-31-2006, 02:02 AM
Hi. This works correctly in IE, but the year won't show in Firefox:

var date;
var day_array = new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
var month_array = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
function show_time()
{
date = new Date();
day = day_array[date.getDay()];
maand = month_array[date.getMonth()];
yand = date.getFullYear();
minutes = date.getMinutes();
seconds = date.getSeconds();
if (minutes < 10 ) {minutes = "0" + minutes;}
if (seconds < 10) {seconds = "0" + seconds;}
obj_table_time = document.getElementById("table_time");
obj_table_time.innerHTML = day + " "+date.getDate()+" " + maand + " " + yand + " "+date.getHours() + ":" + minutes +":" + seconds;
setTimeout("show_time()",1000);
}


********************************
That variable yand doesn't do anything in Mozilla.

Does anyone have any idea why?

Thanks

tpeck
08-31-2006, 02:07 AM
and here's another very weird thing that might be a bug in this forum(???).

See the space in the word "Novem ber".

I didn't post a space - and I double checked that I didn't post that space!

Strange...

_Aerospace_Eng_
08-31-2006, 02:28 AM
You were using date as a variable. Firefox didn't like that because Date is reserved word. This works and it uses DOM to put the time in.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
<script type="text/javascript">
var day_array = new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
var month_array = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
function show_time()
{
var thedate = new Date();
var day = day_array[thedate.getDay()];
var maand = month_array[thedate.getMonth()];
var yand = thedate.getFullYear();
var minutes = thedate.getMinutes();
var seconds = thedate.getSeconds();
if (minutes < 10 ) {minutes = "0" + minutes;}
if (seconds < 10) {seconds = "0" + seconds;}
var obj_table_time = document.getElementById("table_time");
obj_table_time.firstChild.nodeValue = day + " "+thedate.getDate()+" " + maand + " " + yand + " "+thedate.getHours() + ":" + minutes +":" + seconds;
}
window.onload = function()
{
show_time();
setInterval("show_time()",1000);
}
</script>
</head>

<body>
<div id="table_time">&nbsp;</div>
</body>
</html>

jmrker
08-31-2006, 02:37 AM
_Aerospace_Eng_:
You may be right, :o but I can get the original script to work in FF and IE with only the following change: :confused:

<body onLoad="show_time()">
I think 'date' and 'Date' are different to all javascript. :)

_Aerospace_Eng_
08-31-2006, 02:55 AM
_Aerospace_Eng_:
You may be right, :o but I can get the original script to work in FF and IE with only the following change: :confused:
I think 'date' and 'Date' are different to all javascript. :)
Hmm not sure why the year didn't work then though it doesn't hurt to not use variables that have a reserved word in JS.

tpeck
08-31-2006, 01:38 PM
I was using the body onload method, but the year wouldn't show in Firefox. (Everything else did). The whole thing displays if I change "date" to "thedate", so that'll do me.

Thanks a lot.

Still don't know about that interloping space in my first post! I reckon that sort of thing happens a lot in Outlook too. I type something, read it, send it, and... a single character will sometimes just vanish! Of course, it could be early senility. But that space before proves I'm not mad just yet.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum