【问题标题】:Angular Date Display from the Database onto the View in ASP.NET MVC从数据库到 ASP.NET MVC 中的视图的角度日期显示
【发布时间】:2020-06-14 17:17:44
【问题描述】:

我有一个 MVC 应用程序,我从 MVC 控制器获取数据并使用 angular 将其作为 JSON 传递给 View。数据字段之一是日期时间值。在控制器端,日期字段以 {5/27/2019 12:00 AM) 格式显示,但当它在角度端时,相同的日期字段显示例如:/Date(864511200000)/

我已尝试使用将 /Date(864511200000)/ 转换为 {MM/dd/yyyy} 的短日期

 <td>{{employee.DateOfBirth | date:'shortDate'}}</td>

但它不会工作。我的控制器方法

    public JsonResult GetEmployees()
    {
        var employees= db.Employees.ToList();
        return Json(employees, JsonRequestBehavior.AllowGet);
    }

角度代码

myApp.controller('EmployeeCtrl',function ($scope, $http) {

    $http({
        method: 'GET',
        url: '/Employees/GetEmployees'
    }).then(function (response) {
        $scope.employees= response.data;
        console.log(response.data);
    });
});

我的查看代码:

<tr ng-repeat="employee in employees">
        <td>{{employee.DateOfBirth | date:'shortDate'}}</td>

//其余代码

【问题讨论】:

    标签: asp.net angularjs database datetime model-view-controller


    【解决方案1】:

    您需要为日期格式构建自己的自定义过滤器。这是我的代码将在您的场景中为您提供帮助。

    它是 ISO 8601 字符串格式。

    app.filter('inputDate', ['$filter', function ($filter) {
            return function (input, format) {
               return (input) ? $filter('date')(input, format) : '';
            };
    }]);
    

    【讨论】:

      猜你喜欢
      • 2013-09-21
      • 2010-09-22
      • 2022-11-17
      • 1970-01-01
      • 2011-05-07
      • 1970-01-01
      • 2014-10-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多