Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get global time(not the pc time) using javascript or jquery?

I was creating a countdown timer using javascript; I can use jQuery. I want the global time not the PC time.

How could I get the global time using javascript or the jQuery?

like image 725
Harshil Shah Avatar asked Mar 25 '12 18:03

Harshil Shah


People also ask

How to get time of different timezone in JavaScript?

The JavaScript getTimezoneOffset() method is used to find the timezone offset. It returns the timezone difference in minutes, between the UTC and the current local time. If the returned value is positive, local timezone is behind the UTC and if it is negative, the local timezone if ahead of UTC.

How do I get the current time in JavaScript?

In JavaScript, we can easily get the current date or time by using the new Date() object. By default, it uses our browser's time zone and displays the date as a full text string, such as "Fri Jun 17 2022 10:54:59 GMT+0100 (British Summer Time)" that contains the current date, time, and time zone.

What is local time JavaScript?

LocalTime is an immutable date-time object that represents a time, often viewed as hour-minute-second. Time is represented to nanosecond precision. For example, the value '13:45.30. 123456789' can be stored in a LocalTime. It does not store or represent a date or time-zone.


3 Answers

Use a time API like: http://www.timeapi.org/

<script type="text/javascript">
     function myCallback(json) {
          alert(new Date(json.dateString));
     }
</script>
<script type="text/javascript" src="http://timeapi.org/utc/now.json?callback=myCallback"></script>

You can use the UTC methods from Date object: http://www.w3schools.com/jsref/jsref_obj_date.asp

var utcDate = new Date(json.dateString);
alert(utcDate.getUTCFullYear() + '-' + utcDate.getUTCMonth() + utcDAte.getUTCDate());
like image 191
Ricardo Souza Avatar answered Sep 28 '22 09:09

Ricardo Souza


Well unless you make a request to some service that publishes like the current time of an atomic clock or something, you'll have to rely on your computers local time. As JS inherently relies on your local system.

like image 24
JP Silvashy Avatar answered Sep 28 '22 09:09

JP Silvashy


The Date object has built-in methods for getting UTC values. Instead of myDate.getHours() use myDate.getUTCHours(), etc.

See the MDN reference for JavaScript date methods.

like image 36
gilly3 Avatar answered Sep 28 '22 08:09

gilly3