HTML Source box #2 - 3CITIES.HTM

<HTML>
<HEAD>
<TITLE>Javascript Clock</TITLE>
<SCRIPT LANGUAGE = "Javascript">
<!--
//the function to get the time data
function setTime() {
    //initialise variables

    //GMT adjustments for Sydney and San Francisco
    //alter to +11 and -8 in October to account for daylight savings
    //reset to +10 and -7 in March
    sydneyGMTadjustment = 10;
    sanfranciscoGMTadjustment = -7;

    //new date object for your visitors time
    sanfranTime = new Date();
    localTime = new Date();
    sydneyTime= new Date();   

    //build display string for local time
    localTime = buildTimeString(localTime);

    //calculate the local GMT time difference from one date object
    timeDifference = sanfranTime.getTimezoneOffset();

    //adjust sanfranTime to show sanfrancisco time
    sanfranTime.setMinutes(sanfranTime.getMinutes() - timeDifference + sanfranciscoGMTadjustment*60);
    sanfranTime = buildTimeString(sanfranTime);

    //adjust sydneyTime to display Sydney time
    sydneyTime.setMinutes(sydneyTime.getMinutes() - timeDifference + sydneyGMTadjustment*60);
    sydneyTime = buildTimeString(sydneyTime);

    //display these strings in the text boxes on the form
    document.displayClock.sydneytime.value = sydneyTime;
    document.displayClock.sanfranciscotime.value = sanfranTime;
    document.displayClock.localtime.value = localTime;

    //call this function again in 10 seconds time
    setTimeout ("setTime()", 10000)
}

function buildTimeString (currentTime) {
    var currentHours = currentTime.getHours();
    var currentMinutes = currentTime.getMinutes();

    //assume it is the afternoon
    var currentAmPm = "pm"

    //adjust for a 24 hour clock
    if (currentHours > 12)
        currentHours = currentHours - 12;
    else    {

        //set 'am' for hours 0 - 11 inclusive
        if (currentHours <= 11)
            currentAmPm = "am";

        //adjust for midnight
        if (currentHours == 0)
            currentHours = currentHours + 12;
    }
    //pad hours to two digits
    if (currentHours <= 9)
                currentHours = " " + currentHours

    //pad minutes to two digits
    if (currentMinutes <= 9)
        currentMinutes = "0" + currentMinutes
   
    //build a string for the time
    currentTime = currentHours + ":" + currentMinutes + currentAmPm;
    return currentTime;
}
//-->
</SCRIPT>
</HEAD>
<BODY OnLoad = "setTime()" BGCOLOR="#004080" TEXT="#FFEEEE">
<FONT FACE = 'arial'><H1>International times </H1>
This is a simple Javascript application which<br>
reads the system clock and calculates the time in<br>
two cities and displays local time and the time<br>
in these two cities updating it at 10 second intervals.<P>
<!-- The clock is displayed in a text input box on a form -->
<FORM NAME = "displayClock">
<TABLE><TR><TD>
<H2>Your Local Time: <p></TD><TD><INPUT TYPE = "text" NAME = "localtime" SIZE= 6><p></TD></TR><TR><TD>
<H2>Time in San Francisco:<p> </TD><TD><INPUT TYPE = "text" NAME = "sanfranciscotime" SIZE= 6><p></TD></TR><TR><TD>
<H2>Time in Sydney: <p></TD><TD><INPUT TYPE = "text" NAME = "sydneytime" SIZE= 6><p></TD></TR>
</TABLE>
</H2></FORM>

</BODY>
</HTML>

Return to main article