數據庫中數據類型爲datatime,使用AJAX獲取JSON數據時,前端獲取到的將會是一個長度13位的字符串,所以須要作個轉化;前端
//後臺datetime格式轉化 /* 獲取日期格式 */ function getDate(date) { var year = date.getFullYear(); var month = date.getMonth() + 1; var day = date.getDate(); return year + "-" + month + "-" + day ; } /* 獲取日期時間格式*/ function getDateTime(date) { var year = date.getFullYear(); var month = date.getMonth() + 1; var day = date.getDate(); var hh = date.getHours(); var mm = date.getMinutes(); var ss = date.getSeconds(); return year + "-" + month + "-" + day + " " + hh + ":" + mm + ":" + ss; }
或者你也能夠嚴謹一些:數據庫
/* 獲取日期格式 */ function getDate(date) { var year = date.getFullYear(); var month = (date.getMonth() + 1 < 10)? '0' + (date.getMonth() + 1): date.getMonth() + 1; var day = (date.getDate() < 10)? '0' + date.getDate(): date.getDate(); return year + "-" + month + "-" + day ; } /* 獲取日期時間格式*/ function getDateTime(date) { var year = date.getFullYear(); var month = (date.getMonth() + 1 < 10)? ('0' + (date.getMonth() + 1)) : (date.getMonth() + 1); var day = (date.getDate() < 10)? '0' + date.getDate() : date.getDate(); var hh = (date.getHours() < 10)? '0' + date.getHours() : date.getHours(); var mm = (date.getMinutes() < 10)? '0' + date.getMinutes(): date.getMinutes(); var ss = (date.getSeconds() < 10)? '0' + date.getSeconds(): date.getSeconds(); return year + "-" + month + "-" + day + " " + hh + ":" + mm + ":" + ss; }
var date_before = /*後臺返回的數據*/; var date_after = getDateTime(new Date(date_before));