简体   繁体   English

Angular.js格式来自json对象

[英]Angular.js format date from json object

My json response look like this: 我的json响应看起来像这样:

[{"Id":"dab4580b-e24d-49f8-9fd5-2e968b10d3b5","Title":"MVVM-Sidekick 入精","CreatedOn":"\/Date(1390272893353)\/","IsChecked":false},{"Id":"66a0f134-e240-4cc4-96fa-ac3807853ca7","Title":"Windows Phone 开发入精","CreatedOn":"\/Date(1390018447080)\/","IsChecked":false}]

the "CreatedOn" date is in this kind of format: '/Date(1390272893353)/' “CreatedOn”日期采用以下格式:'/ Date(1390272893353)/'

when I bind this result to html table, the date cannot be formatted: 当我将此结果绑定到html表时,日期无法格式化:

<td>{{item.CreatedOn | date: 'yyyy-MM-dd HH:mm'}}</td>

still gives me: 仍然给我:

/Date(1390272893353)/ /日期(1390272893353)/

I don't want to change any code on the server side (don't modify the json string), what's the best way to format this date? 我不想更改服务器端的任何代码(不要修改json字符串),格式化这个日期的最佳方法是什么?

I actually combined the answers from @Charminbear and @Nikos ending up in this filter which works quite nice and is quite clear without the regex: 我实际上把@Charminbear和@Nikos的答案结合在这个过滤器中,这个过滤器工作得非常好,并且在没有正则表达式的情况下非常清楚:

myApp.filter("jsDate", function () {
    return function (x) {
        return new Date(parseInt(x.substr(6)));
    };
});

This makes it possible to write 这使得写入成为可能

{{ $scope.item.CreatedOn | jsDate | date:"yyyy-MM-dd" }}

One option is to write another filter and put it in the chain. 一种选择是编写另一个过滤器并将其放入链中。 Eg: 例如:

app.filter("mydate", function() {
    var re = /\/Date\(([0-9]*)\)\//;
    return function(x) {
        var m = x.match(re);
        if( m ) return new Date(parseInt(m[1]));
        else return null;
    };
});

Basically it uses the regular expression to parse the string and make a Date (if the format is different than the one shown, you will have to tweak the regular expression). 基本上它使用正则表达式来解析字符串并创建一个Date (如果格式与显示的格式不同,则必须调整正则表达式)。

Use it as: 用它作为:

<td>{{item.CreatedOn | mydate | date: 'yyyy-MM-dd HH:mm'}}</td>

I know I'm late for the party. 我知道我迟到了。 But I want to tell what helped me was :- 但我想告诉我有什么帮助: -

<td>{{item.yourdatefield.slice(6,-2) | date:'dd-MMM-yyyy' }}</td>

Hope it'll help lazy coders like Me. 希望它能帮助像我这样懒惰的程序员。 :) :)

The Angular Date-Filter expects a JS-Date. Angular Date-Filter需要一个JS-Date。 So you need to parse the JSON Date before giving it to the filter. 因此,您需要先解析JSON日期,然后再将其提供给过滤器。

Try this: 试试这个:

<td>{{item.CreatedOnParsed | date: 'yyyy-MM-dd HH:mm'}}</td>

And in your callback-method of the response, do something like: 在响应的回调方法中,执行以下操作:

$scope.item.CreatedOnParsed =  new Date(parseInt(item.CreatedOn.substr(6)));

as it is shown in this Answer 正如本答案中所示

EDIT As is just saw by the comments of your post, even just the timestamp is enough for the Date-Filter, so even this should be enough: 编辑正如您的帖子的评论所看到的,即使只是时间戳对于日期过滤器来说已经足够了,所以即使这样也应该足够了:

$scope.item.CreatedOnParsed =  item.CreatedOn.substr(6);
//input - "DocDate":"\/Date(1127318400000-0000)\/"
-------

<tr dir-paginate="user in SalesOrder>
    <td>{{user.DocDate | jsonDate}}</td>
</tr>

controller
----------
app.filter('jsonDate', ['$filter', function ($filter) {
    return function (input, format) {
        return (input) 
               ? $filter('date')(parseInt(input.substr(6)), format) 
               : '';
    };
 }]);

Another, possibly better option, is to parse the date as you recieve the JSON. 另一个可能更好的选择是在收到JSON时解析日期。

I use the following function: 我使用以下功能:

(function() {
    if (JSON && !JSON.parseWithDate) {
        JSON.parseWithoutDate = JSON.parse; //Store the original JSON.parse function

        var reISO = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}(?:\.\d*)?)Z$/;  
        var reMsAjax = /^\/Date\((d|-|.*)\)[\/|\\]$/;  

        JSON.parseWithDate = function (json) {  
            /// <summary>  
            /// parses a JSON string and turns ISO or MSAJAX date strings  
            /// into native JS date objects  
            /// </summary>      
            /// <param name="json" type="var">json with dates to parse</param>          
            /// </param>  
            /// <returns type="value, array or object" />  
            try {  
                var res = JSON.parseWithoutDate(json,
                function (key, value) {  
                    if (typeof value === 'string') {  
                        var a = reISO.exec(value);  
                        if (a)  
                            return new Date(Date.UTC(+a[1], +a[2] - 1,   
                                                     +a[3], +a[4], +a[5], +a[6]));  
                        a = reMsAjax.exec(value);  
                        if (a) {  
                            var b = a[1].split(/[-+,.]/);  
                            return new Date(b[0] ? +b[0] : 0 - +b[1]);  
                        }  
                    }  
                    return value;  
                });  
                return res;  
            } catch (e) {  
                // orignal error thrown has no error message so rethrow with message  
                throw new Error("JSON content could not be parsed");  
                return null;  
            }  
        };  
        JSON.dateStringToDate = function (dtString) {  
            /// <summary>  
            /// Converts a JSON ISO or MSAJAX string into a date object  
            /// </summary>      
            /// <param name="" type="var">Date String</param>  
            /// <returns type="date or null if invalid" />   
            var a = reISO.exec(dtString);  
            if (a)  
                return new Date(Date.UTC(+a[1], +a[2] - 1, +a[3],   
                                         +a[4], +a[5], +a[6]));  
            a = reMsAjax.exec(dtString);  
            if (a) {  
                var b = a[1].split(/[-,.]/);  
                return new Date(+b[0]);  
            }  
            return null;  
        };  
        JSON.stringifyWcf = function (json) {  
            /// <summary>  
            /// Wcf specific stringify that encodes dates in the  
            /// a WCF compatible format ("/Date(9991231231)/")  
            /// Note: this format works ONLY with WCF.   
            ///       ASMX can use ISO dates as of .NET 3.5 SP1  
            /// </summary>  
            /// <param name="key" type="var">property name</param>  
            /// <param name="value" type="var">value of the property</param>           
            return JSON.stringify(json, function (key, value) {  
                if (typeof value == "string") {  
                    var a = reISO.exec(value);  
                    if (a) {  
                        var val = '/Date(' +   
                                  new Date(Date.UTC(+a[1], +a[2] - 1,   
                                           +a[3], +a[4],   
                                           +a[5], +a[6])).getTime() + ')/';  
                        this[key] = val;  
                        return val;  
                    }  
                }  
                return value;  
            })  
        };  
        //Make Date parsing the default
        JSON.parse = JSON.parseWithDate;
    }
})();

Which is taken from here: http://codepaste.net/i89xhc , except that I overwrite the standard JSON.parse function with this version that parses the date. 这是从这里获取的: http//codepaste.net/i89xhc ,除了我用这个解析日期的版本覆盖标准的JSON.parse函数。 This means that Angular will parse ASMX dates automatically from all JSON it sees. 这意味着Angular将从它看到的所有JSON中自动解析ASMX日期。

You could also write a custom transform using angular $http transformations. 您还可以使用angular $ http转换编写自定义转换。 See: https://docs.angularjs.org/api/ng/service/ $http 请参阅: https//docs.angularjs.org/api/ng/service/ $ http

Assuming that is a .net JSON date and you are using moment.js. 假设这是一个.net JSON日期,你使用的是moment.js。 Then leverage it's functionality ( defined here ) into a filter 然后将其功能( 此处定义 )用于过滤器

 myApp.filter('JSONdate', [
       '$filter', function ($filter) {
           return function (input, appformat) {
               if (input != null) {
                   return moment(input).format(appformat);
                   // use the line below if you want to leverage the standard date filter on top of this 
                   // return $filter('date')(new Date(moment(input)), appformat);
               } else {
                   return '';
               }
           };
       }])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM