...

View Full Version : changing clock, change date and time to GMT



Dan13071992
12-14-2011, 12:14 PM
Hi everyone, i made this clock and for me in the UK it displays my time, however for people everywhere else in the world it displays their current time, i would like it to display GMTdate only regardless of where you live. any help would be great. below is my code:



<script type="text/javascript">
<!--

function updateClock ( )
{
var currentTime = new Date ( );

var currentHours = currentTime.getHours ( );
var currentMinutes = currentTime.getMinutes ( );
var currentSeconds = currentTime.getSeconds ( );

// Pad the minutes and seconds with leading zeros, if required
currentMinutes = ( currentMinutes < 10 ? "0" : "" ) + currentMinutes;
currentSeconds = ( currentSeconds < 10 ? "0" : "" ) + currentSeconds;

// Choose either "AM" or "PM" as appropriate
var timeOfDay = ( currentHours < 12 ) ? "AM" : "PM";

// Convert the hours component to 12-hour format if needed
currentHours = ( currentHours > 12 ) ? currentHours - 12 : currentHours;

// Convert an hours component of "0" to "12"
currentHours = ( currentHours == 0 ) ? 12 : currentHours;

// Compose the string for display
var currentTimeString = currentHours + ":" + currentMinutes + ":" + currentSeconds + " " + timeOfDay;

// Update the time display
document.getElementById("clock").firstChild.nodeValue = currentTimeString;
}

// -->
</script>


thanks

Philip M
12-14-2011, 03:20 PM
Have a look at

http://www.w3schools.com/jsref/jsref_obj_date.asp

and then use getUTCHours() etc. (UTC = GMT)

But UTC time is always expressed in the 24-hour clock.


I don't think he was aware that he had a liitle bit more room than he thought. - Commentator ITV1

mrhoo
12-14-2011, 06:48 PM
You can use the UTC properties of the Date object to define your string-


Date.prototype.UTCTime= function(){
var D= this,
A= [D.getUTCHours(), D.getUTCMinutes(), D.getUTCSeconds()];
h= A[0],
w= h< 12? ' am':' pm' ;

if(h> 12) h-= 12;
else if(h== 0) h= 12;
A[0]= h;
for(var i= 0; i<3; i++){
if(A[i]<10) A[i]= '0'+A[i];
}
return A.join(':')+w+ ' GMT';
}

// test
alert(new Date().UTCTime())

Dan13071992
12-14-2011, 07:25 PM
is there an easier way, just to intergrate it into my script there, as its a basic clock, loaded on body onload, and i have no knoledge of javascript at all

Philip M
12-14-2011, 07:54 PM
<html>
<head>

<script type="text/javascript">

function updateClock( ) {
var currentTime = new Date( );

var currentHours = currentTime.getUTCHours ( );
var currentMinutes = currentTime.getUTCMinutes ( );
var currentSeconds = currentTime.getUTCSeconds ( );

// Pad the minutes and seconds with leading zeros, if required
currentMinutes = ( currentMinutes < 10 ? "0" : "" ) + currentMinutes;
currentSeconds = ( currentSeconds < 10 ? "0" : "" ) + currentSeconds;

// Compose the string for display
var currentTimeString = currentHours + ":" + currentMinutes + ":" + currentSeconds

// Update the time display
document.getElementById("clock").innerHTML = currentTimeString + " UTC"

}

</script>

</head>
<body onload = "updateClock()">

<span id = "clock"></span>


</body>
</html>

Obviously this is not a clock which updates continuously to show the current time - only the time onload. You will find plenty of clocks examples in this forum if you use the search facility. You say that you "made this clock" but at the same time you "have no knowledge of Javascript at all". :confused:

Dan13071992
12-14-2011, 07:58 PM
sorry when i said made i should have said that i made it from a tutorial, it has a <body on load> function to update every second aswell, so will that still work with the code you just posted?

Philip M
12-14-2011, 08:01 PM
sorry when i said made i should have said that i made it from a tutorial, it has a <body on load> function to update every second aswell, so will that still work with the code you just posted?

How should I know? Try it! But I see no reason why not. All I have done is changed the script to UTC hours/minutes/sceonds.

rnd me
12-14-2011, 08:03 PM
function updateClock ( )
{

updateClock.tag=updateClock.tag|| document.getElementById("clock");
var m=" AM",
str=(new Date).toGMTString().match(/(\d{2}\:)+\d{2}/g)[0].replace(/^\d+/, function(a,b){
a*=1;
if(a>12){ a= (a-12)||12; m=" PM";}
return ("00"+a).slice(-2);
}) + m;

if(updateClock.last!=str){
updateClock.last=str;
updateClock.tag.firstChild.nodeValue = str;
}
}

setInterval(updateClock, 300);

Philip M
12-14-2011, 08:13 PM
One more time., UTC time is always expressed in the 24-hour clock. You cannot have 3:15pm UTC or whatever. Only 1515 UTC.

Dan13071992
12-14-2011, 08:20 PM
tyhanks guys :) its working now :) just one more quick question, say i have:




<body onLoad="updateClock(); setInterval('updateClock()', 300 )">



thats for one body onload, say i want more than one thing to load on body onload how would i write that into that body onload?

Philip M
12-15-2011, 07:48 AM
<script type="text/javascript">
window.onload = function() {
functionOne();
functionTwo();
functionThree();
}
</script>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum