默认序列化日期为1970至今的时间戳
需要在json.convert中做一些设置
//JavaScriptSerializer js = new JavaScriptSerializer(); IsoDateTimeConverter timeConverter = new IsoDateTimeConverter(); //这里使用自定义日期格式,如果不使用的话,默认是ISO8601格式 //timeConverter.DateTimeFormat = "yyyy'-'MM'-'dd' 'HH':'mm':'ss"; timeConverter.DateTimeFormat = "yyyy'-'MM'-'dd"; return JsonConvert.SerializeObject(t, Formatting.Indented, timeConverter); // string result = js.Serialize(t);
这样格式就是 yyyy-MM-dd了
也可以在js中进行处理
//C#序列化时间转化function ParseDate(item) { var timestamp = item.substring(6, 15); var d = new Date(timestamp * 1000); //根据时间戳生成的时间对象 return (d.getFullYear()) + "-" + (d.getMonth() + 1) + "-" + (d.getDate()) + " " + (d.getHours()) + ":" + (d.getMinutes()) + ":" + (d.getSeconds());}//C#序列化时间转化短日期function ParseShortDate(item) { var time = (ParseDate(item)).substring(0, 9); return time;}