Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript: Is that possible to get the current Unix Timestamp when machine's time is incorrect?

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.

like image 661
Misha Moroshko Avatar asked Nov 28 '25 18:11

Misha Moroshko


1 Answers

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());
like image 115
Smruti Singh Avatar answered Dec 01 '25 06:12

Smruti Singh