Javascript:当机器时间不正确时,是否可以获取当前的 Unix 时间戳? [英] Javascript: Is that possible to get the current Unix Timestamp when machine's time is incorrect?

查看:29
本文介绍了Javascript:当机器时间不正确时,是否可以获取当前的 Unix 时间戳?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我电脑上的时间不正确(比如提前 1 天).

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

在这种情况下,有没有办法获取当前 Unix 时间戳?

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

这个答案 概述了获取 Unix 时间戳的几种方法,但从我所见,他们都假设机器的时间是准确的.

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.

推荐答案

检查这个 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>

您可以使用 Date 对象中的 UTC 方法:http://www.w3schools.com/jsref/jsref_obj_date.asp

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

这篇关于Javascript:当机器时间不正确时,是否可以获取当前的 Unix 时间戳?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆