jQuery/asp.net mvc DateTime 的處理

來源:互聯網
上載者:User

在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/

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.