【问题标题】:ExtJs 4 Displaying WCF DateTimeExtJs 4 显示 WCF 日期时间
【发布时间】:2011-09-07 19:32:08
【问题描述】:

我不知道如何将 wcf 输出的日期时间转换为 ExtJs 可以使用的日期时间。我找到了很多关于此的文章,但它们都是针对 ExtJs 3 的,我无法让它与 4 一起使用。

我确实找到了这段代码,但我不知道如何使用它来转换 JsonStore 中的所有内容。

    //this method is used to convert the MS JSON date format to the ExtJS Grid Date Column Value
function dateFormatter(dt) {
    /// <summary>this method is used to convert the MS JSON date format to the ExtJS Grid Date Column Value</summary>
    /// <param name="dt">Actual JSON Date Value</param>
    try {
        //microsoft JSON date format needs to convert into Javascript date
        var newdata = dt.replace(/\/Date\((-?[0-9]+)([+-][0-9]+)?\)\//g, "new Date($1)");
        newdata = eval('(' + newdata + ')');
        return newdata.format('m/d/Y');
    }
    catch (e) {
        return dt;
    }
}

【问题讨论】:

    标签: asp.net wcf extjs extjs4


    【解决方案1】:

    与 Ext JS 3 不同,Ext JS 4 不扩展本机 Date 对象。相反,它提供 Ext.Date。所以而不是:

    date.format('m/d/Y');
    

    你会改用:

    Ext.Date.format(date, 'm/d/Y');
    

    在大多数情况下,另外使用 eval() 是一个非常糟糕的主意。这段代码也不例外。

    如果你放弃 eval,try-catch 也不需要。

    最后,一个既解析日期又将其转换为另一种格式的函数似乎做得太多了。通常,您会希望在应用程序的不同部分以不同的格式显示相同的日期。因此,我宁愿只拥有一个将 WCF 日期格式解析为 JavaScript Data 对象的函数。然后在需要的地方使用将 Date 对象转换为特定的字符串格式。

    删除所有无关的东西,这就是我得到的:

    function parseWcfDate(dt) {
        var milliseconds = dt.replace(/\/Date\((-?[0-9]+)([+-][0-9]+)?\)\//, "$1");
        return new Date(parseInt(milliseconds, 10));
    }
    

    不管怎样,这一切都太麻烦了……Ext JS 内置了对解析 WCF 格式日期的支持:

    Ext.Date.parse("/Date(1234567894560)/", "MS");
    

    另见:

    【讨论】:

      【解决方案2】:

      将 JSON.NET 与 JavaScriptDateConverter 结合使用。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-07-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-06-12
        • 2011-10-30
        相关资源
        最近更新 更多