1. Hello! You are currently viewing our community as a guest. Register today and apply to be a member of one of the longest standing gaming communities around. Once you have registered learn about our team and how to apply!

javascript code

Discussion in 'General Open/Public Discussion' started by Strygun, 18 Aug 2007.


  1. Hey folks,

    I want to use javascript to create two small clocks to go on my blog. I have found, on the internet, the code to make a simple clock that outputs the current user's time, but I would like to make another one that outputs time in a specific time zone. Could someone take the code I already have and edit it to be configurable into any timezone? (I'll be in London, family in the United States.)

    here's the code I have already: (sorry to use the php vB functions, but when I use code it parses the HTML instead of printing the code.)
    PHP:
    <script type="text/javascript">
    function 
    createtime()
    {
    var 
    time = new Date()
    var 
    hours time.getHours()
    var 
    minutes time.getMinutes()
    var 
    seconds time.getSeconds()
    var 
    abbrev "AM"
    if (hours>=12)
    abbrev="PM"
    if (hours>12)
    {
    hours=hours-12
    }
    if (
    hours==0)
    hours=12
    if (minutes<=9)
    minutes="0"+minutes
    if (seconds<=9)
    seconds="0"+seconds
    var ctime=""+hours+":"+minutes+":"+seconds+" "+abbrev+""
    if (document.all)
    document.all.clock.innerHTML=ctime
    else if (document.getElementById)
    document.getElementById("clock").innerHTML=ctime
    else
    document.write(ctime) }
    if (!
    document.all&&!document.getElementById)
    createtime()
    function 
    loadtime()
    {
    if (
    document.all||document.getElementById)
    setInterval("createtime()",1000)
    }
    </
    script>
    <
    body onLoad="loadtime()">
    <
    center><font size="1.5">Your Time: <span id="clock"></span></font>
    Thanks
     
    Last edited: 18 Aug 2007
  2. c'mon now folks. I know there's someone out there who knows JS. this can't be very hard! you can do it!! :)
     
  3. Hamma

    Hamma Commanding Officer Officer

    Officer
    Coding makes my eyes bleed. :lol:
     
  4. aw c'mon now hamma. I know you can code this thing in seconds. I dare you! ;)
     
  5. I bleed Hamma's eyes daily
     
  6. Hamma

    Hamma Commanding Officer Officer

    Officer
    he also makes my brain esplod :(
     
  7. Just change the timeZone variable to fit yours.

    PHP:
    <script type="text/javascript">
    function 
    createtime()
    {
    var 
    time = new Date()
    var 
    hours time.getUTCHours()
    var 
    minutes time.getUTCMinutes()
    var 
    seconds time.getUTCSeconds()
    var 
    abbrev "AM"
    var timeZone = -5

    if (timeZone != 0)
    hourshours timeZone
    if (hours>=24)
    {
    abbrev="AM"
    hours hours 24
    }
    if (
    hours>=12)
    abbrev="PM"
    if (hours>12)
    {
    hours=hours-12
    }
    if (
    hours==0)
    hours=12

    if (minutes<=9)
    minutes="0"+minutes
    if (seconds<=9)
    seconds="0"+seconds
    var ctime=""+hours+":"+minutes+":"+seconds+" "+abbrev+""
    if (document.all)
    document.all.clock.innerHTML=ctime
    else if (document.getElementById)
    document.getElementById("clock").innerHTML=ctime
    else
    document.write(ctime) }
    if (!
    document.all&&!document.getElementById)
    createtime()
    function 
    loadtime()
    {
    if (
    document.all||document.getElementById)
    setInterval("createtime()",1000)
    }
    </
    script>
    <
    body onLoad="loadtime()">
    <
    center><font size="1.5">Your Time: <span id="clock"></span></font>  
     
  8. yeah but it calls the user's time. How can I know what the offset should be if the users are in different time zones?
     
  9. This one calls GMT/UMT time... 0. So japan is GMT +9 so you put 9 in the timezone variable. Central time would be -5.

    time.getUTCHours()
     

Share This Page