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

我怕爱的太早我们不能终老 提交于 2019-12-10 17:47:17

问题


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.


回答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());



回答2:


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.



来源:https://stackoverflow.com/questions/25439930/javascript-is-that-possible-to-get-the-current-unix-timestamp-when-machines-ti

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!