网络编程 
首页 > 网络编程 > 浏览文章

Javascript将JSON日期格式化

(编辑:jimmy 日期: 2025/9/29 浏览:3 次 )

以下是示例代码

第一种效果:

///无时分秒
function jsonDateFormat(jsonDate) {//json日期格式转换为正常格式
 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日期格式转换为正常格式
 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 "";
 }
}

总结

以上就是Javascript将JSON日期格式化的全部内容,虽然功能很小,但是很实用。希望对大家的学习工作能有所帮助。

上一篇:Jquery组件easyUi实现手风琴(折叠面板)示例
下一篇:Jquery组件easyUi实现选项卡切换示例
友情链接:杰晶网络 DDR爱好者之家 南强小屋 黑松山资源网 白云城资源网 网站地图 SiteMap