I am getting following date while returning from angular code 1/1/0001 in controller. date is displayed in proper format like below
My filter, which is in angularjs
myApp.filter("DateOfBirth", function () {
var re = /\/Date\(([0-9]*)\)\//;
return function (x) {
var m = x.match(re);
if (m)
return new Date(m);
else
return null;
};
});
My controller
public string Update_Data(Employee employee)
{
if(employee != null)
{
var employee_ = projectContext.Entry(employee);
Employee employeeObject = projectContext.Employees.Where(x => x.Employee_ID == employee.Employee_ID).FirstOrDefault();
employeeObject.First_Name = employee.First_Name;
employeeObject.Last_Name = employee.Last_Name;
employeeObject.Country_ID = employee.Country_ID;
employeeObject.State_ID = employee.State_ID;
employeeObject.City_ID = employee.City_ID;
employeeObject.Email = employee.Email;
employeeObject.Password = employee.Password;
employeeObject.Mobile = employee.Mobile;
employeeObject.DateOfBirth =Convert.ToDateTime( employee.DateOfBirth);
employeeObject.Age = employee.Age;
projectContext.SaveChanges();
return "Data Updated";
}
else
{
return "Please Check!!";
}
}
angularjs script
$scope.UpdateEmployee = function (Employee) {
document.getElementById("EmployeeID_").value = Employee.Employee_ID;
$scope.FirstName = Employee.First_Name;
$scope.LastName = Employee.Last_Name;
$scope.counter = Employee.Country_ID;
$scope.Counter = Employee.State_ID;
$scope.CityID = Employee.City_ID;
$scope.Email = Employee.Email;
$scope.Password = Employee.Password;
$scope.DateOfBirth = Employee.DateOfBirth;
$scope.Mobile = Employee.Mobile;
$scope.Age = Employee.Age;
document.getElementById("btnSave").setAttribute("value", "Update");
document.getElementById("btnSave").style.backgroundColor = "Yellow";
document.getElementById("spn").innerHTML = "Update Employee Information";
};
I m getting this value in controller of mvc is
you can simply look for /Date
in the string and convert it, no need to over complicate things.
code:
app.filter('formatDate', function() {
return function(item) {
if (item.indexOf('/Date') != -1)
return new Date(parseInt(item.substr(6)));
return item;
};
});
working sample here
I prefer to move this new Date(parseInt(item.substr(6)));
logic to a service so it be injectable/reusable when needed to a filter/controller.