Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Jul 2010
    Location
    Sheffield
    Posts
    824
    Thanks
    93
    Thanked 18 Times in 18 Posts

    Time based on a timezone

    I have a site where users enter there certain timezome (+1, +4, -8 etc) and i want to display a clock with the local time on their profile page so when people view there profile they can see currently what time it is for them.

    I currently a have this.

    Code:
    function timeclock()
    {
    var t=setTimeout("startclock()",500);
    }
    
    function startclock()
    {
    
    var x = new Date();
    var hour = x.getHours();
    var min = x.getMinutes();
    var sec = x.getSeconds();
    	if (hour<10){hour="0"+hour}
    	if (min<10){min="0"+min}
    	if (sec<10){sec="0"+sec}
    document.getElementById("clock").innerHTML=hour+":"+min+":"+sec;
    timeclock();
    }
    Is it possible to alter the date dependent on the timezone? Maybe put startclock(timezone) andchange it to new Date(timezone);?

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,185
    Thanks
    80
    Thanked 4,453 Times in 4,418 Posts
    Sure.
    Code:
    function startclock( offset )
    {
        var x = new Date();
        x.setTime( x.getTime() + offset * 60 * 60 * 1000 ); // add/subtract offset hours
        ...
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    Regular Coder
    Join Date
    Jul 2010
    Location
    Sheffield
    Posts
    824
    Thanks
    93
    Thanked 18 Times in 18 Posts
    thanks


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •