Correctly format ASP.NET MVC dates for AngularJS

前端 未结 4 1563
灰色年华
灰色年华 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:30

    In your ASP.NET MVC domain model have an extra property that renders the format expected by angular.

     public string CreatedOnJson
             {
                 get
                 {
                     return JsonConvert.SerializeObject(CreatedOn, new JavaScriptDateTimeConverter()).Replace("new Date(","").Replace(")","");
    
                 }
             }
    

    That way you can the use the angular date filter do display in way you prefer:

     {{team.CreatedOn | date:'yyyy-MM' }}
    

提交回复
热议问题