格式化Json传递的日期

Posted SenFanXie

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了格式化Json传递的日期相关的知识,希望对你有一定的参考价值。

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

 1  function ChangeDateFormat(jsondate) {
 2               jsondate = jsondate.replace("/Date(", "").replace(")/", "");
 3               if (jsondate.indexOf("+") > 0) {
 4                   jsondate = jsondate.substring(0, jsondate.indexOf("+"));
 5               } else if (jsondate.indexOf("-") > 0) {
 6                   jsondate = jsondate.substring(0, jsondate.indexOf("-"));
 7               }
 8               var date = new Date(parseInt(jsondate, 10));
 9               var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
10               var currentDate = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
11               return date.getFullYear() + "-" + month + "-" + currentDate;
12           }  

 

以上是关于格式化Json传递的日期的主要内容,如果未能解决你的问题,请参考以下文章