Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    Regular Coder
    Join Date
    Dec 2010
    Location
    Kent, UK
    Posts
    573
    Thanks
    23
    Thanked 10 Times in 10 Posts

    changing clock, change date and time to GMT

    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:

    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
    http://360-tactics.co.uk/forum/index.php

    Crime-Wave

    please post your code wrapped in tags
    please post your PHP wrapped in tags

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,928
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    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
    Last edited by Philip M; 12-14-2011 at 03:34 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #3
    Regular Coder
    Join Date
    Mar 2006
    Posts
    727
    Thanks
    35
    Thanked 132 Times in 123 Posts
    You can use the UTC properties of the Date object to define your string-

    Code:
    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())

  • #4
    Regular Coder
    Join Date
    Dec 2010
    Location
    Kent, UK
    Posts
    573
    Thanks
    23
    Thanked 10 Times in 10 Posts
    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
    http://360-tactics.co.uk/forum/index.php

    Crime-Wave

    please post your code wrapped in tags
    please post your PHP wrapped in tags

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,928
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Code:
    <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".
    Last edited by Philip M; 12-14-2011 at 07:59 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #6
    Regular Coder
    Join Date
    Dec 2010
    Location
    Kent, UK
    Posts
    573
    Thanks
    23
    Thanked 10 Times in 10 Posts
    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?
    http://360-tactics.co.uk/forum/index.php

    Crime-Wave

    please post your code wrapped in tags
    please post your PHP wrapped in tags

  • #7
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,928
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Quote Originally Posted by Dan13071992 View Post
    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.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #8
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,302
    Thanks
    10
    Thanked 586 Times in 567 Posts
    Code:
    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);
    Last edited by rnd me; 12-14-2011 at 08:08 PM.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%

  • Users who have thanked rnd me for this post:

    Dan13071992 (12-14-2011)

  • #9
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,928
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    One more time., UTC time is always expressed in the 24-hour clock. You cannot have 3:15pm UTC or whatever. Only 1515 UTC.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #10
    Regular Coder
    Join Date
    Dec 2010
    Location
    Kent, UK
    Posts
    573
    Thanks
    23
    Thanked 10 Times in 10 Posts
    tyhanks guys its working now just one more quick question, say i have:

    Code:
    <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?
    http://360-tactics.co.uk/forum/index.php

    Crime-Wave

    please post your code wrapped in tags
    please post your PHP wrapped in tags

  • #11
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,928
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Code:
    <script type="text/javascript">
    window.onload = function() {
    functionOne();
    functionTwo();
    functionThree();
    }
    </script>

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


  •  

    Posting Permissions

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