Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the current GMT world time

Tags:

javascript

How can i get the current time? (in JavaScript)

Not the time of your computer like:

now = new Date; now_string = addZero(now.getHours()) + ":" + addZero(now.getMinutes()) + ":" + addZero(now.getSeconds()); 

But the real accurate world time?

Do i need to connect to to a server (most likely yes, which one? and how can i retrieve time from it?)

All the searches I do from google return the (new Date).getHours().

Edit:

I want to avoid showing an incorrect time if the user has a wrong time in his computer.

like image 880
fmsf Avatar asked Jan 28 '09 21:01

fmsf


People also ask

How do you calculate GMT time?

Example: If it is 12 noon at Greenwich, calculate the local time of the places located on the 69o E. 105o E = 105 x 4 = 420 minutes i.e., 420 ÷ 60 = 7 hours Time on Greenwich is 12:00. So, time on 105o E is 12 + 7 hours = 19 or 7 p.m. Q2.

What is current world time zone?

For any given date, the latest place on Earth where it would be valid, is on Howland and Baker Islands, in the IDLW time zone (the Western Hemisphere side of the International Date Line).


1 Answers

You can use JSON[P] and access a time API:

(The code below should work perfectly, just tested it...)

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); }); 
like image 149
James Avatar answered Oct 19 '22 12:10

James