Page 2 of 2 FirstFirst 12
Results 11 to 20 of 20

Thread: How to show server time in java script code?

  1. #11
    Join Date
    Jan 2006
    Posts
    126
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    It does not work.
    This will show not values:
    Current Server Time:
    London:
    Frankfurt:
    Toronto:
    Chicago:
    New York:
    Tokyo:

  2. #12
    Join Date
    Sep 2004
    Location
    Little Falls, Roodepoort, South Africa
    Posts
    421
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default

    Hi

    I take it your want to show the time in various cities around the world?? one way would be to use the getTimezoneOffset() command.. see this thread for a hint:

    time thread
    Very Best Rgds, Simonf :cool:
    __________________________________
    My Site | E-Mail Me

  3. #13
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Sorry, I made a mistake.
    Code:
    if (typeof isHour!="undefined")
      return (num < 10 ? "0" + num : num);
    Simonf: We got past that bit.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  4. #14
    Join Date
    Jan 2006
    Posts
    126
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Can you please tell me the whole code. It seems it is not correct:
    I need for showing cities time as this (format 24h just time without date):
    Please help.
    PHP Code:

    Current Server Time
    :<span id="timecontainer"></span><br />
    London:<span id="timecontainer2"></span><br />
    Frankfurt:<span id="timecontainer3"></span><br />
    Toronto: <span id="timecontainer4"></span><br />
    Chicago: <span id="timecontainer5"></span><br />
    New 
    York: <span id="timecontainer6"></span><br />
    Tokyo: <span id="timecontainer7"></span><br />

    <
    script type="text/javascript">
    new 
    showLocalTime("timecontainer""server-php"0"long")      //GMT -5


    new showLocalTime("timecontainer2""server-php"300"long")  //GMT +1
    new showLocalTime("timecontainer3""server-php"420"long")   //GMT +2
    new showLocalTime("timecontainer4""server-php"0"long")   //GMT -5
    new showLocalTime("timecontainer5""server-php", -60"long")   //GMT -6
    new showLocalTime("timecontainer6""server-php"0"long")   //GMT -5
    new showLocalTime("timecontainer7""server-php"540"long")   //GMT +9


     
    </script> 

  5. #15
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    The third parameter is the offset in minutes, I believe. So GMT +1 should be (5 + 1) * 60 == 360; GMT +9 should be (5 + 9) * 60 = 545.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  6. #16
    Join Date
    Jan 2006
    Posts
    126
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thanks for time. Please help me how to do correct above code for format 24 h.
    So, e.g. 19:45 (Hour:Minutes)

  7. #17
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    If you correct the offsets, that code should be fine.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  8. #18
    Join Date
    Jan 2006
    Posts
    126
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by Twey
    If you correct the offsets, that code should be fine.
    Do you mean code:
    function formatField(num, isHour){
    if (typeof isHour!="undefined"){ //if this is the hour field
    var hour=(num>12)? num-12 : num
    return (hour==0)? 12 : hour
    }
    changed to:

    if (typeof isHour!="undefined")
    return (num < 10 ? "0" + num : num);
    this will not work. Can you please give correct code for this?

  9. #19
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    No,
    Code:
    if (typeof isHour!="undefined"){ //if this is the hour field
    var hour=(num>12)? num-12 : num
    return (hour==0)? 12 : hour
    }
    changed to
    Code:
    if (typeof isHour!="undefined")
    return (num < 10 ? "0" + num : num);
    and
    Code:
    this.container.innerHTML=formatField(hour, 1)+":"+formatField(minutes)+":"+formatField(seconds)+" "+ampm+" ("+dayofweek+")"
    to
    Code:
    this.container.innerHTML=formatField(hour, 1)+":"+formatField(minutes);
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  10. #20
    Join Date
    Jan 2006
    Posts
    126
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question

    I do not know but it does not work.

    I post whole code. Please give me whole code for time format 24hours

    I'm confused.

    PHP Code:
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

    <head>
      <title></title>
    </head>

    <body>
      <script type="text/javascript">

    /***********************************************
    * Local Time script- © Dynamic Drive ([url]http://www.dynamicdrive.com[/url])
    * This notice MUST stay intact for legal use
    * Visit [url]http://www.dynamicdrive.com/[/url] for this script and 100s more.
    ***********************************************/

    var weekdaystxt=["Sun", "Mon", "Tues", "Wed", "Thurs", "Fri", "Sat"]

    function showLocalTime(container, servermode, offsetMinutes, displayversion){
    if (!document.getElementById || !document.getElementById(container)) return
    this.container=document.getElementById(container)
    this.displayversion=displayversion
    var servertimestring=(servermode=="server-php")? '<? print date("F d, Y H:i:s"time())?>' : (servermode=="server-ssi")? '<!--#config timefmt="%B %d, %Y %H:%M:%S"--><!--#echo var="DATE_LOCAL" -->' : '<%= Now() %>'
    this.localtime=this.serverdate=new Date(servertimestring)
    this.localtime.setTime(this.serverdate.getTime()+offsetMinutes*60*1000) //add user offset to server time
    this.updateTime()
    this.updateContainer()
    }

    showLocalTime.prototype.updateTime=function(){
    var thisobj=this
    this.localtime.setSeconds(this.localtime.getSeconds()+1)
    setTimeout(function(){thisobj.updateTime()}, 1000) //update time every second
    }

    showLocalTime.prototype.updateContainer=function(){
    var thisobj=this
    if (this.displayversion=="long")
    this.container.innerHTML=this.localtime.toLocaleString()
    else{
    var hour=this.localtime.getHours()
    var minutes=this.localtime.getMinutes()
    var seconds=this.localtime.getSeconds()
    var ampm=(hour>=12)? "PM" : "AM"
    var dayofweek=weekdaystxt[this.localtime.getDay()]
    this.container.innerHTML=formatField(hour, 1)+":"+formatField(minutes);

    }
    setTimeout(function(){thisobj.updateContainer()}, 60000) //update container every second
    }

    function formatField(num, isHour){
    if (typeof isHour!="undefined")
    return (num < 10 ? "0" + num : num);
    }
    return (num<=9)? "0"+num : num//if this is minute or sec field
    }

    </script>


    Current Server Time:<span id="timecontainer"></span><br />
    London:<span id="timecontainer2"></span><br />
    Frankfurt:<span id="timecontainer3"></span><br />
    Toronto: <span id="timecontainer4"></span><br />
    Chicago: <span id="timecontainer5"></span><br />
    New York: <span id="timecontainer6"></span><br />


    <script type="text/javascript">
    new showLocalTime("timecontainer", "server-php", 0, "long")      //GMT -5


    new showLocalTime("timecontainer2", "server-php", 300, "long")  //GMT +1
    new showLocalTime("timecontainer3", "server-php", 420, "long")   //GMT +2
    new showLocalTime("timecontainer4", "server-php", 0, "long")   //GMT -5
    new showLocalTime("timecontainer5", "server-php", -60, "long")   //GMT -6
    new showLocalTime("timecontainer6", "server-php", 0, "long")   //GMT -5



     </script>

Bookmarks

Posting Permissions

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