English 中文(简体)
以有效. Net 格式以 Javascript 格式指定日期
原标题:Stringify date in javascript in valid .Net format

I have a JSON object on client side that I want to get back on server side.
To do so, I have a hidden in which I put the stringified version of my object.

$("#<%=hidden.ClientID%>").val(JSON.stringify(obj));

然后,在服务器的一面,我试图用JavaScriptSregister去消毒它。

My Problem : the stringified object contains a date and I can t parse it with de JavaScriptSerializer.
What I have done : Modify the date format to make it fit with the .Net format :

function FormatDate(date) {
    if (typeof (date.getTime) != "undefined") {
        return  \/Date(  + date.getTime() +  )\/ 
    }

    return date;
}

这似乎提供了一个很好的格式, 但是, 当我使用 JSON 。 用格式化良好的日期对对象进行刻画时, 它会增加一个额外的反斜线, 所以 JavaScriptSregicizer 仍然无法获得它 。

知道我怎么才能用隐藏的 有效格式得到它吗?

问题回答

我也有同样的问题

 /Date(  + date.getTime() +  )/ ;

works for me. You just have a double backslash instead of only one backslash.

我用下面的代码 来修正数据 在序列后。

var data = JSON.stringify(object);
data = data.replace(/\\/g, "\");

Old question but in case someone arrives here like me looking for a solution, found this that works: https://stackoverflow.com/a/21865563/364568

function customJSONstringify(obj) {
    return JSON.stringify(obj).replace(//Date/g, "\/Date").replace(/)//g, ")\/")
}




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签