![](/img/trans.png)
[英]Javascript resize function for iframes does not work in google chrome
[英]Javascript: Date() function does not fully work in Google Chrome
當我使用Date()函數轉換來自DB的日期時,轉換按預期工作。
這是我的代碼:
$.get(masterUri + "/odata/ViewAppointments")
.done(function (data) {
clubAppointments = new DevExpress.data.ArrayStore({
key: "id"
});
var start, end;
for (var x = 0; x < data.value.length; x++) {
start = new Date(data.value[x].Start_Datetime);
end = new Date(data.value[x].End_DateTime);
clubAppointments.insert({
id: x,
text: data.value[x].Client_Name,
SportId: parseInt(data.value[x].Sport_ID_ref),
Court_ID: parseInt(data.value[x].Court_ID),
startDate: start,
endDate: end,
status: data.value[x].TYPE
});
}
debugger;
});
clubAppointments變量只是DevExtreme JS框架中的ArrayStore。 [不重要]
如果您注意到,我從數組中獲取名為“data.value”的每個值,並將值插入到新的ArrayStore變量中。
在我這樣做之前,我必須將日期轉換為javascript日期,以便我的dxScheduler(Dextreme UI Widget)可以讀取它。
Internet Explorer和FireFox運行良好。 查看剛查看上次約會的結果:
FROM DATABASE [使用Visual Studio調試器] :
data.value [11] .End_DateTime =“2016-05-24T11:30:00”
轉換后 [使用Visual Studio調試器] :
clubAppointments._array [11] .endDate = 2016年5月24日星期二11:30:00 GMT-0400(SA西部標准時間)
這在IE和FireFox上都會發生。
遺憾的是,這個日期轉換在Google Chrome瀏覽器中無效。 請再次觀看結果:
FROM DATABASE : [使用Google Chrome調試器] :
End_DateTime: “2016-05-24T11:30:00”
轉換后 [使用Google Chrome調試器] :
endDate:2016年5月24日星期二07:30:00 GMT-0400(SA西部標准時間)
請查看以下附件,以便清楚地了解最新情況: 圖像
您應該手動解析日期字符串,不要將它留給Date構造函數(或Date.parse,它們是等效的)。 根據ECMAScript 2015,ISO 8601格式的沒有時區的日期和時間字符串應視為本地,但某些瀏覽器會將其視為UTC(如您所見)。
解析日期字符串的唯一可靠方法是手動執行。 如果您只有一種格式,則只需要一個小功能。 如果您有更多要求,小型圖書館可以提供幫助,有很多可供選擇。
例如,這是一個根據當前規范解析ISO 8601格式日期和時間字符串的函數(雖然我不同意日期只將字符串視為UTC,這與ISO 8601和常識不一致)。
/** * Parse an ISO string with or without an offset * eg '2014-04-02T20:00:00-0600' * '2014-04-02T20:00:00Z' * '2014-02' * * Allows decimal seconds if supplied * eg '2014-04-02T20:00:00.123-0600' * * If the string is time and date with no offset, treat as local (per ECMA-262 ed 6) * If date only, eg '2014-04-02' or '2014-02', treat as UTC date (per ECMA-262 ed 6) * * All parts after year are optional * Don't allow two digit years to be converted to 20th century years * @param {string} s - ISO 860 date string */ function parseISOString(s) { var t = s.split(/\\D+/g); var hasOffset = /[-+]\\d{4}$/.test(s); var isZulu = /z$/i.test(s); // Whether decimal seconds are present changes the offset field and ms value var hasDecimalSeconds = /[T ]\\d{2}:\\d{2}:\\d{2}\\.\\d+/i.test(s); var offset = hasDecimalSeconds? t[7] : t[6]; var offSign; var yr = +t[0], mo = t[1]? --t[1] : 0, da = +t[2] || 1, hr = +t[3] || 0, min = +t[4] || 0, sec = +t[5] || 0, ms = hasDecimalSeconds? +t[6] : 0, // Reverse the sign of the offset offSign = hasOffset? /-\\d{4}$/.test(s)? 1 : -1 : 0, offHr = hasOffset? offset/100 | 0 : 0, offMin = hasOffset? offset%100 : 0; // Ensure time values are in range, otherwise invalid date. // Values can't be -ve as splitting on non-digit character if (hr > 24 || min > 59 || sec > 59 || ms > 1000 || offHr > 24 || offMin > 59){ return NaN; } // Create a date object from date parts, check for validity // Avoid two digit years being converted to 20th century var d = new Date(); d.setUTCFullYear(yr, mo, da); // Check that date values are valid if (d.getUTCFullYear() != yr || d.getUTCDate() != da) { return NaN; } // It there's no offset and there are time parts, use local date methods if (!hasOffset && !isZulu && t[3] && t[4] && t[5]) { d.setHours(hr, min, sec, ms); // Otherwise, apply offset to minutes to use UTC date methods } else { min = hasOffset? +min + offSign * (offHr * 60 + offMin) : min; d.setUTCHours(hr, min, sec, ms); } return d; } ['2016-05-24T11:30:00','2016-05-24T11:30:00Z','2016-05-24T11:30:00+0530', '2016-12-04'] .forEach(function(s) { document.write('<br>' + s + '<br>' + parseISOString(s) + '<br>'); });
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.