亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Javascript將JSON日期格式化

 更新時間:2016年08月23日 16:26:13   投稿:daisy  
在做項(xiàng)目中,將實(shí)體轉(zhuǎn)化為JSON后,結(jié)果后臺返回json時間格式為/Date(1306418993027)/,在前臺JS里顯示的并不是真正的日期,而且我們不能把所有日期字段都變成string吧,因此寫了Javascript的擴(kuò)展方法,來實(shí)現(xiàn)這個功能,代碼如下

以下是示例代碼

第一種效果:

///無時分秒
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 "";
 }
}

第二種效果:

///有時分秒
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日期格式化的全部內(nèi)容,雖然功能很小,但是很實(shí)用。希望對大家的學(xué)習(xí)工作能有所幫助。

相關(guān)文章

最新評論