2

Dear all i am using this function to get the GMT time, i am new to java script when i call the function getTime its generating an alert on the webpage as its coded, i want to print to time on the window i try replaced 'alert' by 'document.write' but its printing nothing on the window, any suggestions please:

here is the code:

function getTime(zone, success) {
var url = 'http://json-time.appspot.com/time.json?tz=' + zone,
ud = 'json' + (+new Date());
window[ud]= function(o){
success && success(new Date(o.datetime));
};
document.getElementsByTagName('head')[0].appendChild((function(){
var s = document.createElement('script');
s.type = 'text/javascript';
s.src = url + '&callback=' + ud;
return s;
})());
}
getTime('GMT', function(time){ // This is where you do whatever you want with the time:     
alert(time); });
wael
  • 514
  • 2
  • 8
  • 17

1 Answers1

1

Well you are doing and example but haven't read the entire explanation.

In your case it is normal to just alert the time since you are asking it to do it. If you want a specific GMT date format you could do:

// Get London time, and format it:
getTime('Europe/London', function(time){
var formatted = time.getHours() + ':' 
              + time.getMinutes() + ':'
              + time.getSeconds();
alert( 'The time in London is ' + formatted );
});

EDIT:

If you want to print the time on the window you could add for example a SPAN tag in your HTML with an id let's say 'gmtTime' and have the following:

 // Get London time, and format it:
    getTime('Europe/London', function(time){
    var formatted = time.getHours() + ':' 
                  + time.getMinutes() + ':'
                  + time.getSeconds();
    document.getElementById("gmtTime").innerHTML= formatted;
    });
Bogdan Emil Mariesan
  • 5,529
  • 2
  • 33
  • 57