在Javascript中的DateTime需要使用new Date(318326400000),asp.net mvc返回的Json時間格式變成了/Date(318326400000)/
jQuery.ajax() 函數消費的Json資料的Date類型可以通過jQuery 1.2.6以上版本所增加的 jQuery.ajax.dataFilter
第一步通過jQuery.ajax()的dataFilter函數預先處理asp.net datetime 對象到本地的javascript對象
$.ajax({
type: "POST",
dataType: "json", //資料格式:JSON
url: '/MyProject/SearchMailInfoJson', //目標地址
data: "page=" + pageindx + buildWhere(),
beforeSend: function() { $("#divload").show(); $("#Pagination").hide(); }, //發送資料之前
complete: function() { $("#divload").hide(); $("#Pagination").show(); }, //接收資料完畢
dataFilter: function(data, type) {
return data.replace(/"\\\/(Date\([0-9-]+\))\\\/"/gi, 'new $1');
},
success: function(json) {
$("#list-table tr:gt(0)").remove();
$.each(json, function(i, item) {
if (item["SendTime"] == null) {
item["SendTime"] = "";
}
var trs = "";
trs += "<tr style='font-weight: '> <td align='center'>" + item["EmailSubject"];
trs += "</td><td align='center' style='word-wrap:break-word;word-break:break-all;'>";
trs += item["MessageTo"] + "</td><td>" + item["MessageFrom"] + "</td>";
trs += "<td align='left'>" + item["EmailCC"] + "</td><td align='center'>" + item["MailType"] + "</td>";
trs += "<td align='center'>" + dateFormat(item["ArrivedDateTime"], "yyyy-mm-dd HH:MM:ss") + "</td>";
trs += "<td align='center'>" + item["Status"] + "</td>";
trs += "<td align='center'>" + dateFormat(item["SendTime"], "yyyy-mm-dd HH:MM:ss") + "</td>";
trs += "<td><a href='javascript:showPopWin('效果預覽', 'SendMailPreview?sysId=" + item["SystemID"] + "&mailID=" + item["ID"] + "', 600, 600, null,true,true);>預覽</a></td><tr>";
tbody += trs;
});
$("#list-table").append(tbody);
$("#list-table tr:gt(0)").hover(function() {
$(this).addClass('mouseover');
}, function() {
$(this).removeClass('mouseover');
});
}
});
第二步處理javascript的Date的字串表示,類似於.net DateTime.ToString(). 這可以使用另一個javascript 時間格式庫,文檔參看 http://blog.stevenlevithan.com/archives/date-time-format
http://www.overset.com/2008/07/18/simple-jquery-json-aspnet-webservice-datetime-support/