PDA

View Full Version : Local time script fails in Firefox2 ???



willsoon
06-30-2007, 03:21 AM
Local Time script

http://www.dynamicdrive.com/dynamicindex6/localtime.htm

Having problems with this script displaying in FF2 winxp(sp2)

When copy and pasting sample code into a testpage and running the code on a known ASP server it runsOK in IE 6/7, but comes up with NaN in Firefox.

Can anyone else verify that the exact source code works for ASP in Firefox?
I get the following output: Current Server Time:NaN:NaN:NaN AM (undefined)

Thanks in advance...

jscheuer1
06-30-2007, 04:39 AM
Yes. I just tried it. I copied the the code as instructed on the demo page and uploaded it to a known asp server (not asp.net, as far as I know) and it worked fine in FF 2.0.0.4 under Windows XP Pro 5.12600 Service Pack 2 Build 2600:

filename - local_time_test.asp

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">

/***********************************************
* Local Time script- Dynamic Drive (http://www.dynamicdrive.com)
* This notice MUST stay intact for legal use
* Visit http://www.dynamicdrive.com/ for this script and 100s more.
***********************************************/

var weekdaystxt=["Sun", "Mon", "Tues", "Wed", "Thurs", "Fri", "Sat"]

function showLocalTime(container, servermode, offsetMinutes, displayversion){
if (!document.getElementById || !document.getElementById(container)) return
this.container=document.getElementById(container)
this.displayversion=displayversion
var servertimestring=(servermode=="server-php")? '<? print date("F d, Y H:i:s", time())?>' : (servermode=="server-ssi")? '<!--#config timefmt="%B %d, %Y %H:%M:%S"--><!--#echo var="DATE_LOCAL" -->' : '<%= Now() %>'
this.localtime=this.serverdate=new Date(servertimestring)
this.localtime.setTime(this.serverdate.getTime()+offsetMinutes*60*1000) //add user offset to server time
this.updateTime()
this.updateContainer()
}

showLocalTime.prototype.updateTime=function(){
var thisobj=this
this.localtime.setSeconds(this.localtime.getSeconds()+1)
setTimeout(function(){thisobj.updateTime()}, 1000) //update time every second
}

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

function formatField(num, isHour){
if (typeof isHour!="undefined"){ //if this is the hour field
var hour=(num>12)? num-12 : num
return (hour==0)? 12 : hour
}
return (num<=9)? "0"+num : num//if this is minute or sec field
}

</script>
</head>
<body>
Current Server Time:<span id="timecontainer"></span>

<script type="text/javascript">
new showLocalTime("timecontainer", "server-asp", 0, "short")
</script>
</body>
</html>

If this doesn't clear things up for you, a link to your demo that exhibits the problem may help.

willsoon
07-02-2007, 11:04 PM
Cheers, here's the link to my demo URL (http://www.bleep.co.nz/time.asp)
What have I done wrong?
Thanks again.

jscheuer1
07-03-2007, 02:30 AM
Looks to me like it is working (no longer showing NaN, except in FF), but with:

Current Server Time:2:27:24 AM (Wed)

at 10:27pm (Mon) EDT that's a bit off, even for nz (if that's New Zealand), where it is 2:27pm (Tue) at the moment.

jscheuer1
07-03-2007, 03:01 AM
Ah, I see now, your server is giving the date in this format:

d/m/y h:m:s a.m.

The script expects (on many computers in many browsers, this actually can vary):

m/d/y h:m:s AM

So, I would try adding this red stuff to the script:


this.localtime=this.serverdate=new Date(servertimestring.toUpperCase().replace(/\./g,'').replace(/^(\d*\/)(\d*\/)/,'$2$1'))

willsoon
07-04-2007, 08:28 PM
Ah the joys of non-US date formats!

Hey thanks heaps man - this has been a periodic headache for the last year or so when I first started with this script.

Works perfect in FF now, but would be good if you could check again to see it still displays OK? - NZ is GMT+12 (+/-1 daylight saving).

Thanks so much! :)

jscheuer1
07-05-2007, 03:46 AM
FF on my computer here on the East Coast of the US is now displaying the accurate time for NZ as the server time:

Current Server Time:3:42:28 PM (Thurs)

(11:42pm EDT Wed) here.

(UTC/GMT is 03:42 on Thursday, July 05, 2007) Greenwich