在角MM-DD-YYYY格式显示日期 [英] Show dates in MM-dd-yyyy format in angular

查看:185
本文介绍了在角MM-DD-YYYY格式显示日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

喜正在逐渐鉴于一个无效的日期(即使它在数据库正确的)。

下面是我的项目的一些快照和细节。

在这里输入的形象描述

在这里输入的形象描述

型号: -

 公共部分类员工
    {
        公众诠释标识{搞定;组; }
        公共字符串名称{;组; }
        // [DisplayFormat(ApplyFormatInEditMode = TRUE,DataFormatString ={0:MM / DD / YYYY})]
        //[DataType(DataType.Date)
        公众的DateTime DOB {搞定;组; }
        公共字符串性别{搞定;组; }
        公共字符串电子邮件{获得;组; }
        公共字符串移动{搞定;组; }
        公共字符串地址{搞定;组; }
        公众的DateTime JoiningDate {搞定;组; }
        公众诠释{的DepartmentID获得;组; }
        公众诠释DesignationID {搞定;组; }
    }

JSON功能,使用LINQ从数据库中获得记录: -

 公共JsonResult GETALL()
        {
            使用(empEntities的DataContext =新empEntities())
            {
                在dataContext.Employees VAR EmployeeList的=(给E
                                    加入DEP在dataContext.Departments上E.DepartmentID等于dep.Id
                                    加入DSG在dataContext.Designations上E.DesignationID等于dsg.Id
                                    排序依据E.Id
                                    新选择
                                    {
                                        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;
                返回JsonResult;
            }
        }

查看 -

 < TR DIR-PAGINATE =员工在员工|排序依据:SORTKEY:反转|过滤器:搜索| itemsPerPage:2>
     @ *< TD风格=宽度:100像素;> {{employee.DOB |日期:'DD-MM-YYYY'}}< / TD> * @
     < TD风格=宽度:100像素;> {{employee.DOB |日期:MM-DD-YYYY'在'h:MMA}}< / TD>

角控制器: -

 函数GetAllEmployees(){
    变种的getData = myService.getEmployees();
    调试器;
    getData.then(功能(EMP){
        //emp.DOB =新的日期(emp.DOB);
        $ scope.employees = emp.data;
    },功能(EMP){
        警报(史记聚会失败!);
    });
}


解决方案

我得到了解决。

通过在控制器的变化,它会投中的字符串中的日期。

角控制器: -

 函数GetAllEmployees(){
            变种的getData = myService.getEmployees();
            getData.then(功能(EMP){
                $ scope.employees = emp.data;
                对于(VAR I = 0; I< $ scope.employees.length ++我){
                    $ scope.employees [I] .DOB =新的日期(emp.data [I] .DOB.match(/ \\ D + /)[0] * 1);
                }
            },功能(EMP){
                警报(史记聚会失败!);
            });
        }

型号: -

 公共部分类员工
    {
        公众诠释标识{搞定;组; }
        公共字符串名称{;组; }
        // [DisplayFormat(ApplyFormatInEditMode = TRUE,DataFormatString ={0:MM / DD / YYYY})]
        //[DataType(DataType.Date)
        公众的DateTime DOB {搞定;组; }
        公共字符串性别{搞定;组; }
        公共字符串电子邮件{获得;组; }
        公共字符串移动{搞定;组; }
        公共字符串地址{搞定;组; }
        公众的DateTime JoiningDate {搞定;组; }
        公众诠释{的DepartmentID获得;组; }
        公众诠释DesignationID {搞定;组; }
    }

JSON功能,使用LINQ从数据库中获得记录: -

 公共JsonResult GETALL()
        {
            使用(empEntities的DataContext =新empEntities())
            {
                在dataContext.Employees VAR EmployeeList的=(给E
                                    加入DEP在dataContext.Departments上E.DepartmentID等于dep.Id
                                    加入DSG在dataContext.Designations上E.DesignationID等于dsg.Id
                                    排序依据E.Id
                                    新选择
                                    {
                                        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;
                返回JsonResult;
            }
        }

查看 -

 < TR DIR-PAGINATE =员工在员工|排序依据:SORTKEY:反转|过滤器:搜索| itemsPerPage:2>
     @ *< TD风格=宽度:100像素;> {{employee.DOB |日期:'DD-MM-YYYY'}}< / TD> * @
     < TD风格=宽度:100像素;> {{employee.DOB |日期:MM-DD-YYYY'在'h:MMA}}< / TD>

Hi am getting a invalid date in view(even its correct in db).

Below are some snapshots and details about my project.

Model:-

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 function to get records from database using 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;
            }
        }

View:-

  <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>

Angular Controller:-

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!");
    });
}

解决方案

I got the solution.

By change in controller, it will cast the strings in dates.

Angular Controller:-

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!");
            });
        }

Model:-

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 function to get records from database using 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;
            }
        }

View:-

  <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>

这篇关于在角MM-DD-YYYY格式显示日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆