陈斌彬的技术博客

Stay foolish,stay hungry

JavaScriptSerializer 中日期序列化问题解决方案

解决方案

JavaScriptSerializer serializer = new JavaScriptSerializer();
var json = serializer.Serialize(result);
json = Regex.Replace(json, @"\\/Date\((\d+)\)\\/", match =>
{
    DateTime dtt = new DateTime(1970, 1, 1);
    dtt = dtt.AddMilliseconds(long.Parse(match.Groups[1].Value));
    dtt = dtt.ToLocalTime();
    return dtt.ToString("yyyy-MM-dd HH:mm:ss");
});
Context.Response.Write(json);
Context.Response.End();