Extjs4處理後臺json資料中日期和時間的方法
阿新 • • 發佈:2022-05-03
當ASP.NET後臺使用JavaScriptSerializer這個元件將物件序列化為json,或者使用ScriptMethod特性的json
[ScriptMethod(ResponseFormat = ResponseFormat.Json), WebMethod(EnableSession = true)] public List<EUser> Users()//引數測試用 { List<EUser> l = new List<EUser>(); Random aran = new Random(); for (int i = 0; i < 24; i++) { DateTime date = DateTime.Parse("2015-08-21"); date = date.AddHours(i); var u = new EUser(date,Math.Round(aran.NextDouble() * 100) , Math.Round(aran.NextDouble() * 100), Math.Round(aran.NextDouble() * 100)); l.Add(u); } return l; }
生成的日期json格式是這樣的//Date(1213718400000+0800)//
這種格式ExtJs不識別,導致Grid上無法正常顯示。使用ExtJS4的時候,在列模式裡像下面這樣處理即可。
{ text:'稽核時間', dataIndex:'Date', width:200, renderer: function(value) { if(value){ var dt=eval("new " + value.substr(1, value.length - 2)).toString(); return Ext.util.Format.date(dt, "Y年m月d日H時i分s秒");//"Y年m月d日H時i分s秒" } } }
當ASP.NET後臺使用Newtonsoft.Json(JSON.NET)這個元件將物件序列化為json,
Hashtable ht = new Hashtable();
ht.Add("total", listu.Count);
ht.Add("rows", listu);
JsonStr = JsonConvert.SerializeObject(ht);//使用json.net序列化
context.Response.Write(JsonStr);
生成的日期格式是標準的日期像這樣子:
"2013-01-15T15:00:00"
{
text:'稽核時間',
dataIndex:'Date',
width:200,
xtype:'datecolumn',
format:'Y年m月d日H時i分s秒'
}