• 关于Json传递的日期/Date(数字)/解析


    在将DateTime类型的数据Json后传到前台展示,出现如下效果

    ,在客户端如何解析呢?在jquery easyui 的字段中加一个格式化的函数调用。

      {
    field: 'CreateTime', title: '发布时间',  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 }  
  • 相关阅读:
    Eclipse/MyEclip中使用复制粘贴功能很卡
    ActiveMQ使用教程
    ActiveMQ常见消息类型
    推荐12个最好的 JavaScript 图形绘制库
    eclipse 打包springboot成jar
    CentOS 7下ELK(6.2.4)++LogStash+Filebeat+Log4j日志集成环境搭建
    elasticsearch启动常见错误
    使用haproxy实现负载均衡集群
    linux配置mysql5.6 主主复制同步
    力扣No.102 二叉树的层次遍历
  • 原文地址:https://www.cnblogs.com/gagarinwjj/p/3566338.html
Copyright © 2020-2023  润新知