在javascript模板中格式化json日期

时间:2014-03-23 16:44:41

标签: javascript jquery json jquery-mobile

我的控制器使用JSON将日期数据发送到视图。在视图上,我将这些数据发送到jQuery模板进行显示。我的问题是日期,因为我得到的这个值就像

"/Date(1245398693390)/"

所以我尝试使用this solution但是使用FireBug我可以看到在行处抛出错误

var value = new Date(parseInt(jsonDate.substr(6)));

错误

  

jsonDate.substr is not a function

如果它很重要我正在尝试使用jQuery mobile实现这一点。

更新

<script id="myDataTemplate" type="text/html">          
    <li>@Html.ActionLink("${GetDateString(Date)}", "Fetch", "Data")</li>            
</script>

这是在布局视图中注入的js函数

<script>
            function getDateString(jsonDate) {
                if (jsonDate == undefined) {
                    return "";
                }
                var utcTime = parseInt(jsonDate.substr(6));

                var date = new Date(utcTime);
                var minutesOffset = date.getTimezoneOffset();

                return date.addMinutes(minutesOffset).toString("M/d/yyyy h:mm tt");
            }
        </script>

更新2 这是我最初使用的js函数

function GetDate(jsonDate) {
  var value = new Date(parseInt(jsonDate.substr(6))); //breaks
  return value.getMonth() + 1 + "/" + value.getDate() + "/" + value.getFullYear();
}

1 个答案:

答案 0 :(得分:1)

工作测试

   var aa = "/Date(1245398693390/".match(/\d+/); 

   var bb = parseInt(aa);

   var date = new Date(bb);

   alert(date);

enter image description here