可能的重复: 如何格式化JSON日期?

我的web服务返回一个DateTime到jQuery调用。该服务以以下格式返回数据:

/Date(1245398693390)/

如何将此转换为javascript友好的日期?


当前回答

你可以尝试第三方库,比如json.net,在项目网站上有文档。它说它需要。net 3.5。

还有一种叫做Nii。Json,我相信是从java的一个端口。我在这个博客上找到了一个链接

其他回答

你可以尝试第三方库,比如json.net,在项目网站上有文档。它说它需要。net 3.5。

还有一种叫做Nii。Json,我相信是从java的一个端口。我在这个博客上找到了一个链接

使用string解析日期字符串。替换为反向参考:

var milli = "/Date(1245398693390)/".replace(/\/Date\((-?\d+)\)\//, '$1');
var d = new Date(parseInt(milli));

前面的答案都表明你可以做以下事情:

var d = eval(net_datetime.slice(1, -1));

然而,这在Chrome或FF中都不起作用,因为得到的评估字面上是:

// returns the current timestamp instead of the specified epoch timestamp
var d = Date([epoch timestamp]);

正确的做法是:

var d = eval("new " + net_datetime.slice(1, -1)); // which parses to

var d = new Date([epoch timestamp]); 

如果你在获取时间信息上有困难,你可以尝试这样做:

    d.date = d.date.replace('/Date(', '');
    d.date = d.date.replace(')/', '');  
    var expDate = new Date(parseInt(d.date));

如果你将DateTime从。net代码传递给javascript代码, c#:

DateTime net_datetime = DateTime.Now;

javascript将其视为字符串,如"/Date(1245398693390)/":

你可以把它转换为流动:

// convert the string to date correctly
var d = eval(net_datetime.slice(1, -1))

or:

// convert the string to date correctly
var d = eval("/Date(1245398693390)/".slice(1, -1))