获取服务器时间与本地时间
function ajax(option) { var xhr = null; if (window.XMLHttpRequest) { xhr = new window.XMLHttpRequest(); } else { // ie xhr = new ActiveObject("Microsoft") } // 通过get的方式请求当前文件 xhr.open("get", "http://www.baidu.com"); xhr.send(null); // 监听请求状态变化 xhr.onreadystatechange = function() { var time = null, curDate = null; if (xhr.readyState === 2) { // 获取响应头里的时间戳 time = xhr.getResponseHeader("Date"); console.log(xhr.getAllResponseHeaders()) curDate = new Date(time); console.log("服务器时间: "+curDate); console.log("服务器时间是:" + curDate.getFullYear() + "-" + (curDate.getMonth() + 1) + "-" + curDate.getDate() + " " + curDate.getHours() + ":" + curDate.getMinutes() + ":" + curDate.getSeconds()); var cTime = new Date(); console.log("客户端时间: "+cTime); console.log("客户端时间是:" + cTime.getFullYear() + "-" + (cTime.getMonth() + 1) + "-" + cTime.getDate() + " " + cTime.getHours() + ":" + cTime.getMinutes() + ":" + cTime.getSeconds()); } } } ajax();
本文出自 纳百川,转载时请注明出处及相应链接。
本文永久链接: https://www.bicner.com/1003.html