關於Json傳遞的日期/Date(數字)/解析


在將DateTime類型的數據Json后傳到前台展示,出現如下效果

,在客戶端如何解析呢?在jquery easyui 的字段中加一個格式化的函數調用。

  {
field: 'CreateTime', title: '發布時間', width: 120, formatter: function (value) {                                         return formatNumToDate(value);
}

  具體代碼參見如下:

 

 1 function compareNine(value) {  2     return value > 9 ? value : '0' + value;  3 }  4 function formatNumToDate(value) {  5     var now = eval(value.replace(/\/Date\((\d+)\)\//gi, "new Date($1)"));///.../gi是用來標記正則開始和結束;\是轉義符;()標注了正則匹配分組1,$1 

//直接借助datapattern.js擴展 return now.pattern('yyyy-MM-dd hh:mm:ss');
//或者使用下面方式計算 6 var year = now.getYear() + 1900;//或者 now.getFullYear(); 7 var month = now.getMonth() + 1; 8 var date = now.getDate(); 9 var hour = now.getHours(); 10 var minute = now.getMinutes(); 11 var second = now.getSeconds(); 12 return year + "-" + compareNine(month) + "-" + compareNine(date) + " " + compareNine(hour) + ":" + compareNine(minute) + ":" + compareNine(second); 13 }

 

格式化后效果如下:

嘗試過程:
1    alert(value);//顯示/Date(1392945632000)/
2    //var date = new Date(value.substring(6,19));//直接是字符串,not work
3    //var date = eval("new Date("+value.substring(6, 19)+")");//對字符串加上eval可以work, 但是局限,13位的范圍是[1973-03-03 17:46:40,2286-11-21 01:46:39]
4    //var date = new Date(1392945632000);//直接賦值數字也work
5    var date = eval(value.replace(/\/Date\((\d+)\)\//gi, "new Date($1)"));//通過正則表達式達到通用的目的
6    alert("toGMTString" + date.toGMTString());
7    alert("toDateString" + date.toDateString());
8    alert("toLocaleDateString" + date.toLocaleDateString());
9    alert("toLocaleString" + date.toLocaleString());

 datapattern.js擴展

 1 /**     
 2  * 對Date的擴展,將 Date 轉化為指定格式的String     
 3  * 月(M)、日(d)、12小時(h)、24小時(H)、分(m)、秒(s)、周(E)、季度(q) 可以用 1-2 個占位符     
 4  * 年(y)可以用 1-4 個占位符,毫秒(S)只能用 1 個占位符(是 1-3 位的數字)     
 5  * eg:     
 6  * (new Date()).pattern("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423     
 7  * (new Date()).pattern("yyyy-MM-dd E HH:mm:ss") ==> 2009-03-10 二 20:09:04     
 8  * (new Date()).pattern("yyyy-MM-dd EE hh:mm:ss") ==> 2009-03-10 周二 08:09:04     
 9  * (new Date()).pattern("yyyy-MM-dd EEE hh:mm:ss") ==> 2009-03-10 星期二 08:09:04     
10  * (new Date()).pattern("yyyy-M-d h:m:s.S") ==> 2006-7-2 8:9:4.18     
11  */       
12 Date.prototype.pattern=function(fmt) {        
13     var o = {        
14     "M+" : this.getMonth()+1, //月份        
15     "d+" : this.getDate(), //
16     "h+" : this.getHours()%12 == 0 ? 12 : this.getHours()%12, //小時        
17     "H+" : this.getHours(), //小時        
18     "m+" : this.getMinutes(), //
19     "s+" : this.getSeconds(), //
20     "q+" : Math.floor((this.getMonth()+3)/3), //季度        
21     "S" : this.getMilliseconds() //毫秒        
22     };        
23     var week = {        
24     "0" : "/u65e5",        
25     "1" : "/u4e00",        
26     "2" : "/u4e8c",        
27     "3" : "/u4e09",        
28     "4" : "/u56db",        
29     "5" : "/u4e94",        
30     "6" : "/u516d"       
31     };        
32     if(/(y+)/.test(fmt)){        
33         fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));        
34     }        
35     if(/(E+)/.test(fmt)){        
36         fmt=fmt.replace(RegExp.$1, ((RegExp.$1.length>1) ? (RegExp.$1.length>2 ? "/u661f/u671f" : "/u5468") : "")+week[this.getDay()+""]);        
37     }        
38     for(var k in o){        
39         if(new RegExp("("+ k +")").test(fmt)){        
40             fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));        
41         }        
42     }        
43     return fmt;        
44 }  

 


免責聲明!

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



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