Marko S
Marko S

Reputation: 5307

How to deserialize date format in JSON date string?

This question someone is already asking and solution is with JavaScriptConverter (.NET) but how can I convert normal date into JSON date string with java script.

For example I have a formated date "12-12-2012" and I want to get string something like this example:

/Date(1354316400000+0100)/

Upvotes: 1

Views: 1359

Answers (2)

Matt Johnson-Pint
Matt Johnson-Pint

Reputation: 241728

Icky, awful format, and clumsy slow serializer. (IMHO)

On the server, use Json.Net and its default ISO8601 formatted dates instead.

On the client, use moment.js. It will handle all of the parsing and formatting you could want.

For posterity, if you want to output this format using moment.js, you can do one of these:

moment().format("[/Date](XSSS)/");   // /Date(1198908717056)/

moment().format("[/Date](XSSSZZ)/"); // /Date(1198908717056-0700)/

Upvotes: 1

dshu610
dshu610

Reputation: 191

s = "12-12-2012".split("-");
epoch = Date.parse(s[2] + "-" + s[0] + "-" + s[1]);
output = "/Date(" + epoch + ")/";

if you need the timezone offset, you can use .getTimezoneOffset() on the Date Object and add that to your output string.

Upvotes: 0

Related Questions