Correctly format ASP.NET MVC dates for AngularJS

前端 未结 4 1577
灰色年华
灰色年华 2021-01-05 23:51

I am using AngularJS to get data from an ASP.NET server application and display it on the client side. Here is what I\'m getting:



        
4条回答
  •  忘掉有多难
    2021-01-06 00:25

    The quick fix

    If changing the format in MVC isn't an option, you can do this:

    {{ team.CreatedOn.slice(6, -2) | date: 'yyyy-MM-dd HH:mm:ss' }}
    

    The key bit is .slice(6, -2)—it trims off all the extra junk leaving you with just the epoch time.


    Want a filter?

    If you use this a lot, you may prefer a custom filter. This one wraps the existing date filter:

    .filter('mvcDate', ['$filter', $filter =>
      (date, format, timezone) =>
        date && $filter('date')(date.slice(6, -2), format, timezone)
    ]);
    

    Now just replace the date filter with our custom one:

    {{ team.CreatedOn | mvcDate: 'yyyy-MM-dd HH:mm:ss' }}
    

    Here's a working example on JSFiddle.

提交回复
热议问题