javascript - Weird Date Format Help - Stack Overflow

I created an extension method that uses the built-in ASP.NET serializer to serialize my objects into JS

I created an extension method that uses the built-in ASP.NET serializer to serialize my objects into JSON to send back to my server via AJAX like so:

namespace ExtensionMethods.Json
{
public static class JsonHelper
{
    public static string ToJson(this object obj)
    {
        JavaScriptSerializer serializer = new JavaScriptSerializer();
        return serializer.Serialize(obj);
    }

    public static string ToJson(this object obj, int recursionDepth)
    {
        JavaScriptSerializer serializer = new JavaScriptSerializer();
        serializer.RecursionLimit = recursionDepth;
        return serializer.Serialize(obj);
    }
}
}

//usage
String json = myObject.ToJson();

This works fine, except for dates, as it sends back dates in this format:

/Date(1291276800000)/

Is there a way to fix this serverside so that the date es into something more manageable, or will have to do some stupid character parsing on the client side (ie, scrape the digits out of the parens and try to set a date using that number as milliseconds)? Or is there a better way I'm simply overlooking? I've tried Date.parse([the date]) but it errors out with "Invalid date format".

I created an extension method that uses the built-in ASP.NET serializer to serialize my objects into JSON to send back to my server via AJAX like so:

namespace ExtensionMethods.Json
{
public static class JsonHelper
{
    public static string ToJson(this object obj)
    {
        JavaScriptSerializer serializer = new JavaScriptSerializer();
        return serializer.Serialize(obj);
    }

    public static string ToJson(this object obj, int recursionDepth)
    {
        JavaScriptSerializer serializer = new JavaScriptSerializer();
        serializer.RecursionLimit = recursionDepth;
        return serializer.Serialize(obj);
    }
}
}

//usage
String json = myObject.ToJson();

This works fine, except for dates, as it sends back dates in this format:

/Date(1291276800000)/

Is there a way to fix this serverside so that the date es into something more manageable, or will have to do some stupid character parsing on the client side (ie, scrape the digits out of the parens and try to set a date using that number as milliseconds)? Or is there a better way I'm simply overlooking? I've tried Date.parse([the date]) but it errors out with "Invalid date format".

Share Improve this question asked Dec 3, 2010 at 4:05 JasonJason 52.6k38 gold badges138 silver badges186 bronze badges 5
  • 1 That seems like a valid JSON date to me... I am confused to why is this a problem? – Ilia G Commented Dec 3, 2010 at 4:15
  • @liho1eye - i was unaware this was a standard format, but have figured out a way to fix it :) – Jason Commented Dec 3, 2010 at 18:30
  • 1 really? You figured out a way to fix it, after asking everyone for help, and now you're not going to post how you fixed it to help other people with the same problem? – Ryan Lundy Commented May 12, 2011 at 20:49
  • 1 @kyralessa - i posted the code i wrote to solve this. hope this helps! – Jason Commented May 12, 2011 at 22:07
  • thanks. (For my own purposes I ended up just returning the date I needed as a string, so I didn't have to worry about the weird /Date( stuff.) – Ryan Lundy Commented May 12, 2011 at 22:51
Add a ment  | 

4 Answers 4

Reset to default 4

That is a valid Json Date. Have a look at this other SO question to help you get that date back. How do I format a Microsoft JSON date?

I used JavaScript Date Format date format extension of the Date type. It has worked well with the JSON formatted dates.

I include the .js file and get my dates formatted like this:

function formatJsonDate(jsonDate, formatString) {
    var dt = new Date(+jsonDate.replace(/\/Date\((\d+)\)\//, '$1'));
    return dt.format(formatString);
}

var formattedDate = formatJsonDate(jsonDate, "mm/dd/yyyy");

There are even some predefined date format masks such as :

// Some mon format strings
dateFormat.masks = {
    "default":      "ddd mmm dd yyyy HH:MM:ss",
    shortDate:      "m/d/yy",
    mediumDate:     "mmm d, yyyy",
    longDate:       "mmmm d, yyyy",
    fullDate:       "dddd, mmmm d, yyyy",
    shortTime:      "h:MM TT",
    mediumTime:     "h:MM:ss TT",
    longTime:       "h:MM:ss TT Z",
    isoDate:        "yyyy-mm-dd",
    isoTime:        "HH:MM:ss",
    isoDateTime:    "yyyy-mm-dd'T'HH:MM:ss",
    isoUtcDateTime: "UTC:yyyy-mm-dd'T'HH:MM:ss'Z'"
};

I had this problem too and decided to just move all of my date objects over to Unix timestamps and parse them back. It's extra work, but it keeps the funny formatting out. If you have a class variable of the long datatype it should hold a timestamp pretty nicely.

There's some pretty solid samples over here.

http://www.epochconverter./

For the answer to this, this is basically what I used:

function parseJsonDate (date, shortFormat) {
    if (date != null) {
        var d = new Date(parseInt(date.substr(6)));
            if (shortFormat) {
                return (d.getMonth() + 1) + '/' + d.getDate() + '/' +
                          d.getFullYear().toString().substr(2);
            }
            return d;
    } else {
            return null;
    }
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745170141a4614874.html

相关推荐

  • javascript - Weird Date Format Help - Stack Overflow

    I created an extension method that uses the built-in ASP.NET serializer to serialize my objects into JS

    13小时前
    50

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信