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

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...


The solution is within the string shown in the question. The JavaScript Date object will parse that format and produce a readable version so Date(1319266795390+0800) returns "Wed Apr 18 2012 08:13:22 GMT-0500 (Central Daylight Time)".

To remove the forward slash from the string you could use the replace function with a regular expression: "/Date(1319266795390+0800)/".replace(///g, '') .


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();
});

你可以使用这个:

string date = "/Date(1319266795390+0800)/";
string regex = @"/Date((.*?)+(.*?))/";
Match match = Regex.Match(date, regex);
DateTime d = new DateTime(1970, 01, 01).AddMilliseconds(long.Parse(match.Result("$1")));
链接地址: http://www.djcxy.com/p/46658.html

上一篇: 在Firebase中保存和检索日期

下一篇: 将字符串中的JSON日期替换为更具可读性的日期