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.
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.
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).
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); });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With