标签:blog io ar os sp java for on 2014
在做项目中,将实体转化为JSON后,结果后台返回json 时间格式为/Date(1306418993027)/,在前台JS里显示的并不是真正的日期,而且我们不能把所有日期字段都变成string吧,因此找了一个JS的扩展方法,来实现这个功能,代码如下:function ChangeDateFormat(jsondate) { jsondate = jsondate.replace("/Date(", "").replace(")/", ""); if (jsondate.indexOf("+") > 0) { jsondate = jsondate.substring(0, jsondate.indexOf("+")); } else if (jsondate.indexOf("-") > 0) { jsondate = jsondate.substring(0, jsondate.indexOf("-")); } var date = new Date(parseInt(jsondate, 10)); var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1; var currentDate = date.getDate() < 10 ? "0" + date.getDate() : date.getDate(); var hours=date.getHours() < 10 ? "0" + date.getHours():date.getHours(); var minutes=date.getMinutes() < 10 ? "0" +date.getMinutes():date.getMinutes(); var seconds=date.getSeconds() <10 ?"0" + date.getSeconds():date.getSeconds(); return date.getFullYear() + "年" + month + "月" + currentDate + "日" + " " + hours + ":" + minutes +":" +seconds;}用ajax调用:
$.ajax({ type: "Post", textType: "json", url: "/UserInfo/GetDate", data: { id: id }, success: function (data) { alert(ChangeDateFormat(data.AddTime)); } });
标签:blog io ar os sp java for on 2014
原文地址:http://blog.csdn.net/lvshihua/article/details/41629607