一佳一

记录像1+1一样简洁的代码

导航

JavaScriptSerializer 时间格式化

Posted on 2014-08-23 10:12  一佳一  阅读(426)  评论(0编辑  收藏  举报

时间格式化

Model m = new Model { Id = 1, Dt = DateTime.Now };  
        JavaScriptSerializer js = new JavaScriptSerializer(); 

        string str = js.Serialize(m); 

        str = Regex.Replace(str, @"\\/Date\((\d+)\)\\/", match =>  

        { 

            DateTime dt = new DateTime(1970, 1, 1); 

            dt = dt.AddMilliseconds(long.Parse(match.Groups[1].Value)); 

            dt = dt.ToLocalTime(); 

           return dt.ToString("yyyy-MM-dd HH:mm:ss"); 

        }); 

        Response.Write(str); 

//{"Id":1,"Dt":"2011-08-17 17:38:47"}