I want to display the current date and time in UTC that the sever works with. How I can force javascript to work with the right date and time! I just want Javascript to show the right dates, in UTC, that the server works with - regardless of what the client's time/date settings are (i.e 12hrs ahead time zone or date set wrongly etc.). Is this possible?
问问题
1211 次
3 回答
0
如果您想在客户端上执行 javascript 时使用服务器的日期时间,则服务器必须动态地将其时间提供给客户端,或者作为单独的 http 请求,或者与调用 javascript 的网页一起。
于 2013-09-11T08:28:05.347 回答
0
这将为您提供 UTC 日期
var now = new Date();
var now_utc = new Date(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate(), now.getUTCHours(), now.getUTCMinutes(), now.getUTCSeconds());
于 2013-09-11T06:43:19.440 回答
0
我在我的 aspx 页面中添加了一个 js 文件(date.js)。并编写一个函数来显示日期时间。
<div id="clock">
</div>
function UpdateUTCDateTime() {
var currentDate = Date.now();
var currentUTCdate = currentDate.toUTCString();
if (navigator.userAgent.indexOf("MSIE") < 0) {
currentUTCdate = currentUTCdate.replace("GMT", "UTC");
}
$("#Clock").text(Date.parseExact(currentUTCdate, "ddd, dd MMM yyyy HH:mm:ss UTC").toString("dd-MMM-yyyy hh:mm tt"));
setTimeout(UpdateUTCDateTime, (60 - currentDate.getSeconds()) * 1000);
}
所以它会给我当前的UTC日期时间。
于 2013-12-06T12:03:36.640 回答