以下是示例代碼
第一種效果:
///無(wú)時(shí)分秒function jsonDateFormat(jsonDate) {//json日期格式轉(zhuǎn)換為正常格式 try { var date = new Date(parseInt(jsonDate.replace("/Date(", "").replace(")/", ""), 10)); var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1; var day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate(); return date.getFullYear() + "-" + month + "-" + day; } catch (ex) { return ""; }}
第二種效果:
///有時(shí)分秒function jsonDateFormat(jsonDate) {//json日期格式轉(zhuǎn)換為正常格式 try { var date = new Date(parseInt(jsonDate.replace("/Date(", "").replace(")/", ""), 10)); var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1; var day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate(); var hours = date.getHours(); var minutes = date.getMinutes(); var seconds = date.getSeconds(); var milliseconds = date.getMilliseconds(); return date.getFullYear() + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds + "." + milliseconds; } catch (ex) { return ""; }}
總結(jié)
以上就是Javascript將JSON日期格式化的全部?jī)?nèi)容,雖然功能很小,但是很實(shí)用。希望對(duì)大家的學(xué)習(xí)工作能有所幫助。
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注