(转载或分享请注明链接,谢谢!)
有时候在后台序列化数据的时候,包含date类型的字段经过序列化后传过来的json里的日期会变成/Date(1498297711000 0800)/这种类型的日期格式,前台可以通过javascript进行日期格式化,利用jquery的replace先进行替换操作,之后再转成date类型,最后格式化成自己想要的类型:
function ChangeDateFormat(cellval) { var date = new Date(parseInt(cellval.replace("/Date(", "").replace(")/", ""), 10)); //getMonth()从0开始算 var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1; var currentDate = date.getDate() < 10 ? "0" + date.getDate() : date.getDate(); return date.getFullYear() + "-" + month + "-" + currentDate; }
转换完成后的日期格式为yyyy-MM-dd.