PDA

View Full Version : GMT Time Display Script?



blueberry
11-08-2005, 03:06 AM
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>

jscheuer1
11-08-2005, 05:43 AM
Should work, didn't want to reset my clock to test it.


<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>

mwinter
11-09-2005, 01:24 PM
if (curHour = 24) curHour = 0That 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. :D

The original code could have also been changed so that



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



if (curHour >= 24) curHour -= 24;


Anyway, the simpler approach is:



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