如何通过JavaScript从Internet获取当前日期

use*_*400 2 javascript

我有这个功能:

function fun_voting_started(){
var now = new Date();
var now_date = now.getDate();
var now_month = now.getMonth(); now_month++;
var now_year = now.getFullYear();
var now_hours = now.getHours();
var now_min = now.getMinutes();
var now_sec = now.getSeconds();
var voting_started = now_year + "-" + now_month + "-" + now_date + " " + now_hours + ":" + now_min + ":" + now_sec; 
return voting_started;
}
Run Code Online (Sandbox Code Playgroud)

此功能从计算机加载日期,但有时用户使用的日期不是当前日期。如何从互联网获取当前日期?

Abh*_*lks 5

纯粹从PoC角度回答问题标题“ 如何通过JavaScript从Internet获取当前日期 ”,无论它有多有用或无用。

您可以从Internet时间服务器(或与此有关的任何服务器)获取时间,该时间服务器会发布API来执行此操作。您可以在网上搜索找到一个。

例如(this)和(this)

这些服务支持使用Javascript进行AJAX回调的JSONP。

演示:http://jsfiddle.net/abhitalks/n66eP/

JS:

$.ajax({
    dataType: 'jsonp',
    url: 'http://timeapi.org/utc/now.json',
    success: function (result) {
        alert(result.dateString);
    }
});
Run Code Online (Sandbox Code Playgroud)

该服务返回带有日期字符串的对象。只需要在这里访问dateString属性。

  • 我不能说不再起作用了。 (5认同)
  • @ user3357400:好吧,这是另一天的问题:)在SO上已经问了很多遍了。请搜索。 (3认同)