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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Displaying Dates By Time Zone

Status
Not open for further replies.

light742

Technical User
Aug 9, 2010
15
US
Hello:

I'm a newbie to javascript and am trying to get a file to correctly display the time for different time zones. Unfortunately, the time is off by one hour. I have attached 2 files. Thank you.

The 1st file is:

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
        "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">[/URL] 

<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml">[/URL]

<head> 

        <title>Time Zones</title> 
        <script type="text/javascript" src="JavaScript_Ch11_script04_Page267.js"></script> 

</head> 

<body bgcolor="#FFFFFF">

<!-- Filename: C:\BLUE\JavaScript_Ch11_script04_Page266.html  -->
        <h3>Our office hours are 9:00 am to 5:00 pm, Monday through Friday, at each of our locations. It is now</h3> 

        <ul> 
            <li><span class="tz-8"></span> in San Francisco</li> 
            <li><span class="tz-5"></span> in New York</li> 
            <li><span class="tz-0"></span> in London</li> 
            <li><span class="tz+7"></span> in Hong Kong</li> 
        </ul> 
</body> 

</html>

The 2nd file is:

Code:
window.onload = initDate; 

function initDate() { 
        var allTags = document.getElementsByTagName("*"); 

        for (var i=0; i<allTags.length; i++) { 
                if (allTags[i].className.indexOf("tz")==0) { 
                        showTheTime(allTags[i],allTags[i].className.substring(2)); 
                } 
        } 
}

// Filename is C:\BLUE\JavaScript_Ch11_script04_Page267.js

function showTheTime(currElem,tzOffset) { 
        var dayName = new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"); 

        var thatTZ = new Date(); 
        var dateStr = thatTZ.toUTCString(); 

        dateStr = dateStr.substr(0,dateStr.length - 3); 
        thatTZ.setTime(Date.parse(dateStr)); 
        thatTZ.setHours(thatTZ.getHours() + parseInt(tzOffset)); 

        currElem.innerHTML = showTheHours(thatTZ.getHours()) + showZeroFilled(thatTZ.getMinutes()) + showAmPm(thatTZ.getHours()) + dayName[thatTZ.getDay()]; 
        function showTheHours(theHour) { 
                if (theHour == 0) { 
                        return 12; 
                } 
                if (theHour < 13) { 
                        return theHour; 
                } 
                return theHour-12; 
        } 
        function showZeroFilled(inValue) { 
                if (inValue > 9) { 
                        return ":" + inValue; 
                } 
                return ":0" + inValue; 
        } 
        function showAmPm(thatTime) { 
                if (thatTime < 12) { 
                        return " AM "; 
                } 
                return " PM "; 
        } 
}
 
Hi

I would say, the problem is that [tt]toGMTString()[/tt] ( by the way, it is deprecated ) returns UTC time. And UTC is not affected by daylight saving. So those -8, -7, 0, +7 will work fine during the winter and will fail only on summer. Even more, there you are displaying times from 3 continents, and daylight saving transition is not done in the same weekend all over the world.

So better search for a dedicated server-side solution, which can take in account such differences based on a database.


Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top