jquery 解析數據庫中的json日期為正常的格式


//在action從后台數據庫中請求獲得日期以后,得到的是json格式的數據,因此要解析才能顯示在前台
1.在jsp頁面寫的代碼如下
<html> <script> Date.prototype.format = function(format) { var o = { "M+": this.getMonth() + 1, // month "d+": this.getDate(), // day "h+": this.getHours(), // hour "m+": this.getMinutes(), // minute "s+": this.getSeconds(), // second "q+": Math.floor((this.getMonth() + 3) / 3), // quarter "S": this.getMilliseconds() // millisecond } if (/(y+)/.test(format)) format = format.replace(RegExp.$1, (this.getFullYear() + "") .substr(4 - RegExp.$1.length)); for (var k in o) if (new RegExp("(" + k + ")").test(format)) format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length)); return format; } </script> <body> <script> var date = new Date(); console.info(date); console.info(date.format("yyyy-MM-dd hh:mm")); </script> </body> </html>

 

   2. 上述示例代碼中,我們還是需要指定format,這里先抽象出一個formatDatebox函數,用於顯示日期.

 在js中寫的代碼如下:

 function formatDatebox(value) {
            if (value == null || value == '') {
                return '';
            }
            var dt;
            if (value instanceof Date) {
                dt = value;
            } else {

                dt = new Date(value);

            }

            return dt.format("yyyy-MM-dd"); //擴展的Date的format方法(上述插件實現)
        }

測試方法:
console.info(formatDatebox(1402367297000));//括號中的內容可以等價替換為數據庫中請求得到的日期json數據


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM