JSON日期时间的处理

 

什么是 JSON ?

 

  • JSON 指的是 JavaScript 对象表示法(JavaScript Object Notation)
  • JSON 是轻量级的文本数据交换格式
  • JSON 独立于语言 *
  • JSON 具有自我描述性,更易理解

 

* JSON 使用 JavaScript 语法来描述数据对象,但是 JSON 仍然独立于语言和平台。JSON 解析器和 JSON 库支持许多不同的编程语言。

JSON格式不直接支持日期和时间,对于JSON格式的日期和时间可通过JavaScript进行如下处理:

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; 
 }

posted @ 2013-06-07 18:40  C&Y Across I  阅读(809)  评论(0编辑  收藏  举报