下面的代碼在除IE8以外的任何瀏覽器中都能正常工作。我正在從服務器獲取時間,並嘗試在IE8中顯示日期字符串和服務器小時。我得到的是字符串serverHour,undefined undefined undefined undefined,對於小時,testHours,我得到了nan。我曾嘗試使用moment.js來獲取顯示日期,但我得到了相同的結果。任何指導將不勝感激。我試過用不同的方式重新設置日期字符串的格式,但是我找不到能夠工作的組合。我必須錯過一些非常基本的東西。Javascript日期顯示nan在ie8
var xmlHttp;
var offset = 0;
var today = new Date();
/*
return the standard time timezone offset regardless of whether the current time is on standard or daylight saving time.
http://www.webdeveloper.com/forum/showthread.php?228309-Getting-server-date-time-with-no-server-side-script
*/
Date.prototype.stdTimezoneOffset = function() {
var jan = new Date(this.getFullYear(), 0, 1);
var jul = new Date(this.getFullYear(), 6, 1);
return Math.max(jan.getTimezoneOffset(), jul.getTimezoneOffset());
}
/*
Determine if the current time is on daylight saving time or not. We simply compare the current timezone offset with the standard one.
If they are equal then the current time is standard time. If they are not then the current time is daylight saving time. This second
method will return true when the current time is daylight saving time and false when it is standard time.
http://www.webdeveloper.com/forum/showthread.php?228309-Getting-server-date-time-with-no-server- side-script
*/
Date.prototype.dst = function() {
return this.getTimezoneOffset() < this.stdTimezoneOffset();
}
// Convert GMT server time to Pacific time and return date.
function getServerTime(serverDateMs,offset) {
var date = new Date(serverDateMs + offset * 3600 * 1000);
return date;
}
// Function to get server time in GMT
function srvTime() {
// Create an XML object to collect information from server
try {
//FF, Opera, Safari, Chrome
xmlHttp = new XMLHttpRequest();
}
catch (err1) {
//IE
try {
xmlHttp = new ActiveXObject('Msxml2.XMLHTTP');
}
catch (err2) {
try {
xmlHttp = new ActiveXObject('Microsoft.XMLHTTP');
}
catch (err3) {
//AJAX not supported, use CPU time.
alert("AJAX not supported");
}
}
}
// Request information from the server using XML object
xmlHttp.open('POST', window.location.href, false);
xmlHttp.send();
return xmlHttp.getResponseHeader("Date");
}
// Set offset if daylight savings time or standard time
if (today.dst() == true) { offset = -7; }
else { offset = -8; }
var x = srvTime();
//i have: Fri, 02 Jan 2015 22:54:05 GMT
// modify the string to remove comma and GMT
var dateString = x.replace(",", "").replace("GMT", "");
var a = dateString.split(" ");
//I want: Nov 06 2012 23:29:33 +0000
// reorganize to match above format
var newDatString = a[2] + " " + a[1] + " " + a[3] + " " + a[4];
// create the date object
var serverT = new Date(newDatString);
// get date/time in milliseconds
var serverDateMs = serverT.getTime();
// convert GMT time to pacific time
var serverDatePacific = getServerTime(serverDateMs, offset);
// create date object from pacific time
var serverHour = new Date(serverDatePacific);
// get hours from pacific time date object
var testHour = serverHour.getHours();
什麼是'serverDateMs'? – Jack
可能有'window.location.href.toString()'的問題? http://stackoverflow.com/questions/9466156/tostring-does-not-work-in-ie –
serverDateMS是服務器時間/日期轉換爲毫秒。由於我無法得到任何服務器信息顯示在ie8客戶端我停止調用函數來測試。 – vadorian