11

我刚刚开始使用jQuery 模板作为我的 javascript 模板引擎。我的问题是,我如何格式化日期(从 ASP.NET Json ActionResult 返回)格式为:

/Date(1288709830000)/

我尝试执行以下操作:

{{= $.format(new Date(parseInt(comment.DateCreated.substr(6))), 'd')}} 

注意上面使用了新的jquery全球化插件来添加$.format方法。另请注意,这{{= comment.DateCreated }}是长手的说法${comment.DateCreated}

如果您能提供帮助,我将不胜感激。

4

4 回答 4

3

这是我用的

var formatDate = function (datetime) {
    var dateObj = new Date(parseInt(datetime.replace("/Date(", "").replace(")/", ""), 10));
    return dateObj.format("dd-MMM-yyyy"); //01-Jun-2001
}

这在我的 JQuery 模板中

${格式日期(起始日期)}

于 2011-10-07T09:35:49.167 回答
2

这确实有效。我使用的是 Microsoft CDN 上托管的测试版。如果您下载最新版本,一切都会按预期工作。

于 2010-12-07T21:48:20.017 回答
1

我想出了一个非常hacky的解决方案。如果在页面中添加以下函数:

function format(o, t) {
    return $.format(o, t);
}

然后,您可以将表达式更改为:

{{= format(new Date(parseInt(comment.DateCreated.substr(6))), 'd') }}

它工作正常。微软创建的两个插件会以这种方式发生冲突,这似乎很奇怪。

于 2010-12-03T09:10:25.820 回答
0

要在 jQuery 模板中格式化日期时间,您可以编写如下函数:

function formatDate(datetime) {
    var dateObj = new Date(datetime);
    var dateStr = (dateObj.getMonth()+1) + "/" + dateObj.getDate() + "/" + dateObj.getFullYear();
    return dateStr; // will return mm/dd/yyyy
}

然后,您可以从 jQuery 模板中调用此函数,如下所示:${formatDate(comment.DateCreated)}

有关详细信息,请参阅:http ://api.jquery.com/template-tag-equal

于 2011-01-16T21:56:25.850 回答