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