3

Suppose that the time on my computer is incorrect (say 1 day ahead).

Is there a way to get the current Unix Timestamp in this case?

This answer outlines few way to get the Unix Timestamp, but from what I can see they all assume that machine's time is accurate.

Community
  • 1
  • 1
Misha Moroshko
  • 166,356
  • 226
  • 505
  • 746
  • 1
    Yes, you get it from the server instead, either with ajax, or just by echoing it somewhere. – adeneo Aug 22 '14 at 05:11
  • No, I don't guess so. If e.g. the hardware clock is wrong, there is only this information on the machine and no knowledge about this fault. The only thing I could imagine is a call over HTTP (or maybe even NTP) from the JavaScript file to a server where you can be sure it has the correct time. – ConcurrentHashMap Aug 22 '14 at 05:15
  • 1
    get from server side (s1) remember the client time (c1), next time in c2 you want the server time, you can use s1+c2-c1, but this is not so accurate if you want the precision of milliseconds – powerfj Aug 22 '14 at 05:18

2 Answers2

1

Check for this api 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());
Smruti Singh
  • 565
  • 1
  • 4
  • 14
  • 1
    The **only** value for *json.dateString* that is reliable is a time value in milliseconds, any other value will be dependent on *Date.parse*, which is implementation dependant. [*w3schools*](http://www.w3fools.com) is a poor reference site, use either the [*ECMAScript specification*](http://ecma-international.org/ecma-262/5.1/#sec-15.9.1.1) where authority is required or [*MDN*](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date) for examples. – RobG Aug 22 '14 at 06:14
0

I have a site specifically for such reasons: http://www.currentmillis.com/ You can make a request to the indicated PHP pages and you'll get back a UNIX timestamp, either in seconds or milliseconds.

Sandman
  • 2,577
  • 2
  • 21
  • 32