Extjs中对日期的处理
renderer: function(value) { if (Ext.isEmpty(value)) {//判断是否是日期类型的数据 return ‘‘; } else { if (Ext.isDate(value)) return Ext.util.Format.date(value, ‘Y-m-d H:i:s‘);// 用于时间控件返回值 else return value;// 转换为Date类型 } } }
1、Extjs中对日期的校验---使用正则表达式:
var value ="2014-03-03"; var re = /^(\d{4})(\/|-)(\d{1,2})\2(\d{1,2})$/ var m = re.exec(value); if (m != null) { var d = new Date(m[1],m[3]-1,m[4]); var falg = d.getFullYear()==m[1] && d.getMonth()==(m[3]-1) && d.getDate()==m[4]; } if(value==‘‘||value.length < 1){ this.msgTarget = "qtip"; this.invalidText = ‘必填项不能为空!‘; return false; }else if(!falg){ this.msgTarget = "qtip"; this.invalidText = ‘格式不正确!‘; return false; }
2、Extjs在grid列表中的处理----显示为NAN
renderer: function(value) { if (Ext.isEmpty(value)) {//判断是否是日期类型的数据 return ‘‘; } else { if (Ext.isDate(value)) return Ext.util.Format.date(value, ‘Y-m-d H:i:s‘);// 用于时间控件返回值 else return value;// 转换为Date类型 } } }
3、Extjs计算日期差:
var indate = record.get("inDate"); indate = indate.replace(/-/g,"/"); value = value.replace(/-/g,"/"); var inHours = new Date(indate); var FinishHours = new Date(value); var day = (FinishHours - inHours)/(24*60*60*1000);//天数 var hours = (FinishHours - inHours)/(60*60*1000);//小时 if(day>=0 && hours > 2){ return ‘<font color=blue></font><span >‘ + value + ‘</span>‘; }else{ return value; }
附:在遇到问题2,如果后台使用了JsonArray来处理对象,那么可以使用下面的方法:
JsonConfig jsonConfig = new JsonConfig(); jsonConfig.setExcludes(new String[]{ "invoice","creatorId", "modifyDate", "modifyId","modifyName","createDate","modifyDate","creatorName" }); jsonConfig.registerJsonValueProcessor(java.sql.Timestamp.class,new JsonValueProcessor() { private final String format="yyyy-MM-dd"; public Object processObjectValue(String key, Object value,JsonConfig arg2){ if(value==null) return ""; if (value instanceof Timestamp) { String str = new SimpleDateFormat(format).format((Timestamp) value); return str; } return value.toString(); } public Object processArrayValue(Object value, JsonConfig arg1){ return null; } }); JSONArray json = JSONArray.fromObject(inventoryList,jsonConfig);
package com.sinosoft.pmhy.util.conver; import java.util.Date; import org.apache.commons.lang.time.DateFormatUtils; import net.sf.json.JSONObject; import net.sf.json.JsonConfig; import net.sf.json.processors.JsonBeanProcessor; public class MyJsDateJsonBeanProcessor implements JsonBeanProcessor { public static String DEFAULT_DATE_PATTERN = "yyyy-MM-dd HH:mm"; public static String processDate(Date date) { return DateFormatUtils.format(date, DEFAULT_DATE_PATTERN); } private String datePattern = null; public String getDatePattern() { if (datePattern == null) { return DEFAULT_DATE_PATTERN; } else { return datePattern; } } public void resetDatePattern() { datePattern = null; } public JSONObject processBean(Object bean, JsonConfig jsonConfig) { if (bean instanceof java.sql.Date) { java.sql.Date d = (java.sql.Date) bean; long time = d.getTime(); String pattern = getDatePattern(); String date = DateFormatUtils.format(time, pattern); return makeJSONObject(date, time, pattern); } if (bean instanceof Date) { Date d = (Date) bean; long time = d.getTime(); String pattern = getDatePattern(); String date = DateFormatUtils.format(time, pattern); return makeJSONObject(date, time, pattern); } return new JSONObject(true); } private static JSONObject makeJSONObject(String date, long time, String pattern) { JSONObject jsonObject = new JSONObject(); jsonObject.element("date", date); jsonObject.element("time", time); jsonObject.element("pattern", pattern); return jsonObject; } }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。