2016-06-21 55 views
0

您好,我正在查看無效日期(即使它在db中正確)。如何以角度顯示MM-dd-yyyy格式的日期

下面是關於我的項目的一些快照和詳細信息。

enter image description here

enter image description here

型號: -

public partial class Employee 
    { 
     public int Id { get; set; } 
     public string name { get; set; } 
     //[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy}")] 
     //[DataType(DataType.Date)] 
     public DateTime DOB { get; set; } 
     public string Gender { get; set; } 
     public string Email { get; set; } 
     public string Mobile { get; set; } 
     public string Address { get; set; } 
     public DateTime JoiningDate { get; set; } 
     public int DepartmentID { get; set; } 
     public int DesignationID { get; set; } 
    } 

JSON功能使用LINQ從數據庫中的記錄來獲得: -

public JsonResult getAll() 
     { 
      using (empEntities dataContext = new empEntities()) 
      { 
       var employeeList = (from E in dataContext.Employees 
            join dep in dataContext.Departments on E.DepartmentID equals dep.Id 
            join dsg in dataContext.Designations on E.DesignationID equals dsg.Id 
            orderby E.Id 
            select new 
            { 
             E.Id, 
             E.name, 
             E.DOB, 
             E.Gender, 
             E.Email, 
             E.Mobile, 
             E.Address, 
             E.JoiningDate, 
             dep.DepartmentName, 
             E.DepartmentID, 
             dsg.DesignationName, 
             E.DesignationID 
            }).ToList(); 
       var JsonResult = Json(employeeList, JsonRequestBehavior.AllowGet); 
       JsonResult.MaxJsonLength = int.MaxValue; 
       return JsonResult; 
      } 
     } 

查看: -

<tr dir-paginate="employee in employees|orderBy:sortKey:reverse|filter:search|itemsPerPage:2"> 
    @*<td style="width: 100px;">{{employee.DOB |date:'dd-MM-yyyy'}}</td>*@ 
    <td style="width: 100px;">{{employee.DOB | date:"MM-dd-yyyy 'at' h:mma"}}</td> 

角控制器: -

function GetAllEmployees() { 
    var getData = myService.getEmployees(); 
    debugger; 
    getData.then(function (emp) { 
     //emp.DOB = new Date(emp.DOB); 
     $scope.employees = emp.data;   
    }, function (emp) { 
     alert("Records gathering failed!"); 
    }); 
} 
+2

JSON中的約定是對日期使用ISO 8601格式。 ASP.NET MVC以這種格式返回日期。 '/ Date(..)'從哪裏來?在任何情況下,如果你想改變* display *格式,你需要解析字符串(例如用'new Date()'),然後格式化它,但是你想要 –

回答

-1

我得到了解決方案。

通過控制器的更改,它將在日期中投射字符串。

角控制器: -

function GetAllEmployees() { 
      var getData = myService.getEmployees();  
      getData.then(function (emp) {   
       $scope.employees = emp.data; 
       for (var i = 0; i < $scope.employees.length; ++i) {     
        $scope.employees[i].DOB = new Date(emp.data[i].DOB.match(/\d+/)[0] * 1); 
       } 
      }, function (emp) { 
       alert("Records gathering failed!"); 
      }); 
     } 

型號: -

public partial class Employee 
    { 
     public int Id { get; set; } 
     public string name { get; set; } 
     //[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy}")] 
     //[DataType(DataType.Date)] 
     public DateTime DOB { get; set; } 
     public string Gender { get; set; } 
     public string Email { get; set; } 
     public string Mobile { get; set; } 
     public string Address { get; set; } 
     public DateTime JoiningDate { get; set; } 
     public int DepartmentID { get; set; } 
     public int DesignationID { get; set; } 
    } 

JSON功能使用LINQ to從數據庫中獲得記錄: -

public JsonResult getAll() 
     { 
      using (empEntities dataContext = new empEntities()) 
      { 
       var employeeList = (from E in dataContext.Employees 
            join dep in dataContext.Departments on E.DepartmentID equals dep.Id 
            join dsg in dataContext.Designations on E.DesignationID equals dsg.Id 
            orderby E.Id 
            select new 
            { 
             E.Id, 
             E.name, 
             E.DOB, 
             E.Gender, 
             E.Email, 
             E.Mobile, 
             E.Address, 
             E.JoiningDate, 
             dep.DepartmentName, 
             E.DepartmentID, 
             dsg.DesignationName, 
             E.DesignationID 
            }).ToList(); 
       var JsonResult = Json(employeeList, JsonRequestBehavior.AllowGet); 
       JsonResult.MaxJsonLength = int.MaxValue; 
       return JsonResult; 
      } 
     } 

查看: -

<tr dir-paginate="employee in employees|orderBy:sortKey:reverse|filter:search|itemsPerPage:2"> 
    @*<td style="width: 100px;">{{employee.DOB |date:'dd-MM-yyyy'}}</td>*@ 
    <td style="width: 100px;">{{employee.DOB | date:"MM-dd-yyyy 'at' h:mma"}}</td> 
1

那是因爲日期類型實際上並不存在的JSON。

所以,你必須首先做什麼,是你的字符串鑄造成一個日期:

在你的角度控制器,你可以簡單地做到以下幾點:

function GetAllEmployees() { 
    var getData = myService.getEmployees(); 
    debugger; 
    getData.then(function (emp) { 
     //emp.DOB = new Date(emp.DOB); 
     $scope.employees = emp.data; 
     for (var i = 0; i < $scope.employees.length; ++i){ 
      $scope.employees[i].DOB = new Date(emp.DOB); 
     } 

    }, function (emp) { 
     alert("Records gathering failed!"); 
    }); 
} 

編輯:

你也可以創建一個這樣的攔截器:

(function() { 
    window.Company = window.Company || {}; 
    Company.DateTime = Company.DateTime || {}; 
    var parseDate = function(textValue) { 
     return new Date(textValue); 
    }; 

    Company.DateTime.parseObject = function(dataObject) { 
     if (_.isString(dataObject) && /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?(?:Z|(?:\+|\-)?\d{1,2}:\d{2})$/.test(dataObject.trim())) { 
      dataObject = parseDate(dataObject.trim()); 
     } 
     if (_.isArray(dataObject)) { 
      for (var i = 0, l = dataObject.length; i < l; i++) { 
       dataObject[i] = Company.DateTime.parseObject(dataObject[i]); 
      } 
     } 
     if (_.isObject(dataObject)) { 
      for (var prop in dataObject) { 
       if (dataObject.hasOwnProperty(prop)) { 
        dataObject[prop] = Company.DateTime.parseObject(dataObject[prop]); 
       } 
      } 
     } 
     return dataObject; 
    }; 
}()); 
+0

hi @Deblaton在控制器中投射的位置,我的控制器上面給出 –

+0

@SunilChaudhry你不會將日期轉換爲字符串,你*格式*他們的字符串(非常強大的提示)。 –

+0

@Deblaton現在它顯示爲空作爲日期(當我alert(emp.DOB);它顯示未定義,甚至警報(emp.data);也未定義) –