奇怪的日期格式帮助

发布于 2024-10-06 10:44:01 字数 876 浏览 3 评论 0原文

我创建了一个扩展方法,它使用内置的 ASP.NET 序列化程序将我的对象序列化为 JSON,以便通过 AJAX 发送回我的服务器,如下所示:

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

/Date(1291276800000)/

这工作正常,但日期除外,因为它以这种格式发送回日期 有一种方法可以修复此服务器端,以便日期变得更易于管理,或者必须在客户端进行一些愚蠢的字符解析(即,从括号中刮掉数字并尝试使用该数字设置日期作为毫秒)?还是有更好的方法我只是忽略?我尝试过 Date.parse([the date]) 但出现错误并显示“无效的日期格式”。

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

烟织青萝梦 2024-10-13 10:44:01

这是一个有效的 Json 日期。看看这个其他问题,以帮助您找回该日期。 如何格式化 Microsoft JSON 日期?

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?

烛影斜 2024-10-13 10:44:01

我使用了 Date 类型的 JavaScript Date Format 日期格式扩展。它与 JSON 格式的日期配合得很好。

我包含 .js 文件并获取如下格式的日期:

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

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

甚至还有一些预定义的日期格式掩码,例如:

// Some common 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 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 common 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'"
};
朦胧时间 2024-10-13 10:44:01

我也遇到了这个问题,决定将所有日期对象移至 Unix 时间戳并解析它们。这是额外的工作,但它可以避免有趣的格式。如果您有一个long数据类型的类变量,它应该很好地保存时间戳。

这里有一些非常可靠的样本。

http://www.epochconverter.com/

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

毁梦 2024-10-13 10:44:01

对于这个问题的答案,这基本上是我使用的:

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

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;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文