Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

replace a JSON date in a string to a more readable date

Tags:

json

c#

regex

We want to show some JSON to a user who is testing our application. So we call our REST service in the ASP.NET code behind file and return a string, which holds a lot of JSON.

We then put it in a PRE element in the page, call beautify to create nice readable JSON and all is good: sort of human readable content is shown.

Good but for one thing: all the dates are shown in the normal JSON format like this "/Date(1319266795390+0800)/"

What I want to do is replace those JSON dates with 'normal' dates, in the JSON (C#) string, so in the code behind that is, before I add the string to the PRE element.

I was thinking about some regex, but i couldn't figure out how...

like image 538
Michel Avatar asked Apr 18 '12 12:04

Michel


People also ask

How do I parse a date in JSON?

Use the toJSON() method to get a string representation of the Date object. Pass the JSON string to the Date() constructor. The Date() constructor will parse the ISO string and will create a Date object.

How do I set date format in JSON?

There is no date format in JSON, there's only strings a de-/serializer decides to map to date values. However, JavaScript built-in JSON object and ISO8601 contains all the information to be understand by human and computer and does not relies on the beginning of the computer era (1970-1-1).

Does JSON Stringify convert date to UTC?

stringify(gridUpdates) is converting the date values to UTC. Since the usage of JSON.

Can JSON be a date?

JSON does not have a built-in type for date/time values. The general consensus is to store the date/time value as a string in ISO 8601 format.


1 Answers

I'v been dealing with dates in JSON string for some time now, there's no standard way for that and which is why there are so many different ways to do it! Maybe it was better if JSON specification could specify an standard format for dates in the first place!

Microsoft is doing it in its own way, counting the msecs since 1970 in UTC format this is something like "/Date(1319266795390+0800)/"

We've been changing the above string to ISO-8601 format ever since using Regular Expressions on top of ASP.Net JavaScriptSerializer output. It is a W3C standard, human readable and the way most browsers serialize Date to string, here's how:

static readonly long DATE1970_TICKS = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc).Ticks;
static readonly Regex DATE_SERIALIZATION_REGEX = new Regex(@"\\/Date\((?<ticks>-?\d+)\)\\/", RegexOptions.Compiled);

static string ISO8601Serialization(string input)
{
    return DATE_SERIALIZATION_REGEX.Replace(input, match =>
    {
        var ticks = long.Parse(match.Groups["ticks"].Value) * 10000;
        return new DateTime(ticks + DATE1970_TICKS).ToLocalTime().ToString("yyyy-MM-ddTHH:mm:ss.fff");
    });
}

You can easily change the format to satisfy your needs, to see custom Date and Time formats check out MSDN article here

Here's how it's used:

JavaScriptSerializer ser = new JavaScriptSerializer();
var JsonSrt = ISO8601Serialization(ser.Serialize(DateTime.Now));    // "\"2012-05-09T14:51:38.333\""

Update:

There's an alternative to tweak the JSON string returned from the server in JavaScript to more readable form using Regex:

var str = "/Date(1319266795390+0800)/";
str.replace(/\/Date\((\d+)\+\d+\)\//, function (str, date) {
    return new Date(Number(date)).toString();
});
like image 56
Kamyar Nazeri Avatar answered Sep 21 '22 20:09

Kamyar Nazeri