javascript - 在javascript模板中格式化json日期

标签 javascript jquery json jquery-mobile

我的 Controller 使用 JSON 将日期数据发送到 View 。在 View 中,我将这些数据发送到 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>

这是在布局 View 中注入(inject)的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();
}

最佳答案

工作测试

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

   var bb = parseInt(aa);

   var date = new Date(bb);

   alert(date);

enter image description here

关于javascript - 在javascript模板中格式化json日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22593788/

相关文章:

javascript - For 字符串内循环以连接变量中的字符(不使用 RegExp)- Javascript

javascript - while( ) 中的加法

php - 字幕没有完全滚动

JQuery AJAX 响应仅返回 [object Object] 且在 C# ASP.NET 中未定义

javascript - XMLHttpRequest 无法加载 http 无 Access-Control-Allow-Origin header angularjs

javascript 使用 console.log 而不移至新行

JQUERY 切换和 Ajax

c# - 如何防止在 C# 反序列化过程中初始化 JSON 字符串中不存在的属性?

php - json_decode() 返回错误 "Notice: Trying to get property of non-object"

javascript - 为什么结果是getId :f() instead of 123?