代码改变世界

Extjs怎么格式化Grid的日期列

2009-07-03 13:21  午夜瞎想  阅读(8452)  评论(0编辑  收藏  举报

在Grid的cm里面定义列的时候使用renderer 属性进行修改日期格式.由于在用.net把List<T>转成json的时候会把日期类型转化成不是常用的格式所以我们需要利用js把他转换过来.

第一步需要把他转换成js认识的Date格式:new Date(parseInt(val.substring(6, val.length - 2)))

第二步再把Date按照你的要求进行转化,我这里是扩展了一个format方法

   1:  Date.prototype.format = function(format) {
   2:      var o =
   3:                  {
   4:                      "M+": this.getMonth() + 1, //month
   5:                      "d+": this.getDate(),    //day
   6:                      "h+": this.getHours(),   //hour
   7:                      "m+": this.getMinutes(), //minute
   8:                      "s+": this.getSeconds(), //second
   9:                      "q+": Math.floor((this.getMonth() + 3) / 3), //quarter
  10:                      "S": this.getMilliseconds() //millisecond
  11:                  }
  12:   
  13:      if (/(y+)/.test(format))
  14:          format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
  15:      for (var k in o)
  16:          if (new RegExp("(" + k + ")").test(format))
  17:          format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
  18:      return format;
  19:  }

下面是具体Grid日期列的定义

   1:  {dataIndex : 'BillDate',
   2:          header : '用电日期',
   3:          hidden : false,
   4:          renderer : function (val) {
   5:      return new Date(parseInt(val.substring(6, val.length - 2))).format('yyyy-MM-dd')
   6:      }
   7:      ,
   8:          sortable : false
   9:      }