Results 1 to 3 of 3

Thread: GMT Time Display Script?

  1. #1
    Join Date
    Nov 2005
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default GMT Time Display Script?

    GMT Time Script:
    http://www.crays.com/jsc/jsC4Udate.htm

    Shown below is a script that displays GMT 24-hour time, which should switch from 23:59 to 00:00 at 12 midnight.

    However, instead of changing to 00:00, it goes on to show 24:00 - not a big deal, but we know it’s a little funky-looking for some visitors.

    At 1am, it finally changes to 01:00…

    We would really appreciate it if you could fix this? (Or you might know a better one?)

    Thanks so much!!!
    Blueberry

    ---------------------------------

    <SCRIPT Language="JavaScript">
    <!-- hide from old browsers
    var curDateTime = new Date()
    var curHour = curDateTime.getHours()
    + curDateTime.getTimezoneOffset()/60
    if (curHour > 24) curHour -= 24
    if (curHour < 0) curHour += 24
    var curMin = curDateTime.getMinutes()
    var curSec = curDateTime.getSeconds()
    var curTime =
    ((curHour < 10) ? "0" : "") + curHour + ":"
    + ((curMin < 10) ? "0" : "") + curMin + ":"
    + ((curSec < 10) ? "0" : "") + curSec
    document.write(curTime + " GMT")
    //-->
    </SCRIPT>

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Should work, didn't want to reset my clock to test it.

    Code:
    <script type="text/javascript">
      var curDateTime = new Date()
      var curHour = curDateTime.getHours() 
         + curDateTime.getTimezoneOffset()/60
      if (curHour > 24)  curHour -= 24
      if (curHour < 0) curHour += 24
      if (curHour = 24)  curHour = 0
      var curMin = curDateTime.getMinutes()
      var curSec = curDateTime.getSeconds()
      var curTime = 
        ((curHour < 10) ? "0" : "") + curHour + ":" 
        + ((curMin < 10) ? "0" : "") + curMin + ":" 
        + ((curSec < 10) ? "0" : "") + curSec 
      document.write(curTime + " GMT") 
    </script>
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Dec 2004
    Location
    UK
    Posts
    2,358
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by jscheuer1
    if (curHour = 24) curHour = 0
    That would always set curHour to zero as assignment of 'true' values (those other than an empty string, boolean false, zero [0], null, and undefined) will always evaluate to true.

    I know; it was a typo.

    The original code could have also been changed so that

    Code:
    if (curHour > 24) curHour -= 24
    became:

    Code:
    if (curHour >= 24) curHour -= 24;
    Anyway, the simpler approach is:

    Code:
    var now = new Date(),
        h   = now.getUTCHours(),
        m   = now.getUTCMinutes(),
        s   = now.getUTCSeconds();
    
    document.write((h < 10 ? '0' : '') + h + ':'
      + (m < 10 ? '0' : '') + m + ':'
      + (s < 10 ? '0' : '') + s + ' GMT');
    The UTC methods return the time without timezone/DST compensation so no adjustments are necessary at all.

    Mike

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
  •