首页 热点资讯 义务教育 高等教育 出国留学 考研考公

如何格式化JSON时间对象

发布网友

我来回答

2个回答

懂视网

自定义日期类型转化函数ChangeDateFormat,将//Date(1294499956278+0800)//形式的函数转化为2014-5-2形式


function ChangeDateFormat(jsondate) {
  jsondate = jsondate.replace("/Date(", "").replace(")/", "");
  if (jsondate.indexOf("+") > 0) {
   jsondate = jsondate.substring(0, jsondate.indexOf("+"));
  } else if (jsondate.indexOf("-") > 0) {
   jsondate = jsondate.substring(0, jsondate.indexOf("-"));
  }
  var date = new Date(parseInt(jsondate, 10));
  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;
  }

热心网友

通过json获取的时间对象和javascript通过new Date()创造的对象是有区别的的,json的时间对象如下所示:
var d = {"date":20,"day":2,"hours":2,"minutes":57,"month":2,"seconds":39,"time":1332212259490,"timezoneOffset":0,"year":112}
我们可以获取d的time属性,并将它传递给new Date(),重新构造时间对象,如下:
var date = new Date(d.time);
这样就可以使用javascript的方式操作json返回的时间了

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com