<rt id="hkid1"></rt>
    <s id="hkid1"><meter id="hkid1"></meter></s>
  1. <rp id="hkid1"><meter id="hkid1"></meter></rp>
    <source id="hkid1"></source>
  2. <ruby id="hkid1"><optgroup id="hkid1"></optgroup></ruby>

    Extjs4處理后臺json數據中日期和時間的方法

    小編:管理員 237閱讀 2022.09.06

    當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秒' 
    }
    復制
    關聯標簽:
    快三群