views:

37

answers:

1
<script type="text/javascript">
//Live Javascript Server Time
function getthedate(city, offset){
    // create Date object for current location
    d = new Date();

    // convert to msec
    // add local time zone offset 
    // get UTC time in msec
    utc = d.getTime() + (d.getTimezoneOffset() * 60000);

    // create new Date object for different city
    // using supplied offset
    nd = new Date(utc + (3600000*offset));


    var hours = nd.getHours()
    var minutes = nd.getMinutes()
    var seconds = nd.getSeconds()
    var dn="AM"
        if (hours>=12)
        dn="PM"
        if (hours>12){
        hours=hours-12
        hours="0"+hours
        }
        if (hours==0)
        hours=12
        if (minutes<=9)
        minutes="0"+minutes
        if (seconds<=9)
        seconds="0"+seconds

    var cdate="<strong><font color='b9b9b9' size='1'> "+hours+":"+minutes+":"+seconds+" "+dn+"</font></strong>"
    if (document.all)
    document.all.clock.innerHTML=cdate
    else if (document.getElementById)
    document.getElementById("clock").innerHTML=cdate
    else
    document.write(cdate)
    }
if (!document.all&&!document.getElementById)
getthedate()
function live_servertime(){
if (document.all||document.getElementById)
setInterval("getthedate()",1000)
 }
 // get London time
alert(getthedate('London', '+1'));   </script>

Hey. I wanted to display the live time of a server (if I know its timezone/utc offset) by using this hack on a simple browser live time script: Javascript hack link

However, using my above code, all that is displayed is: NaN:NaN:Nan AM Does anyone know how to make this work?

A: 

I don't get NaN:NaN:Nan AM or something like that, it just seems to work for me.

But there are some flaws in your code. First, always declare your variables locally using var. Now you're silently overwriting d, utc and nd in the global namespace.

Second, this is most likely not what you want:

if (hours>12){
    hours=hours-12
    hours="0"+hours
}

You don't want a 0 added before the hour if it's between 10 PM and midnight. Moreover, that 0 isn't added when it's before 10 AM.

You probably want to test this outside the hours>12 condition:

if (hours<=9)
    hours="0"+hours;

Furthermore, you do

if (document.all)
document.all.clock.innerHTML=cdate

document.all is very old, you don't have to use it anymore! Even IE 6 understands the standard document.getElementById.

setInterval("getthedate()",1000)

Always try to feed setInterval and setTimeout a function instead of a string. To quote MDC:

Using this syntax is not recommended for the same reasons as using eval()

And last, but not least, use proper indenting and put a semicolon after each statement; to quote Douglas Crockford:

JavaScript uses a C-like syntax which requires the use of semicolons to delimit statements. JavaScript attempts to make semicolons optional with a semicolon insertion mechanism. This is dangerous.

Like C, JavaScript has ++ and -- and ( operators which can be prefixes or suffixes. The disambiguation is done by the semicolon.

In JavaScript, a linefeed can be whitespace or it can act as a semicolon. This replaces one ambiguity with another.

JSLint expects that every statement be followed by ; except for for, function, if, switch, try, and while. JSLint does not expect to see unnecessary semicolons or the empty statement.

It's hard to follow your code the way you wrote it; for instance, I trapped myself thinking that your condition (hours>12) was within the AM/PM condition.

Marcel Korpel
thank you for the help
Callum Johnson
- Problem sorted.
Callum Johnson