Hi mahesh213,
While fetching record from JsonResult in Controller the Date field is converted to Json Date format. So you need to first convert the Json Date to DateTime format and then assign it to the scope variable.
Refer below link for details.
Check this example. Now please take its reference and correct your code.
Controller
public class HomeController : Controller
{
// GET: /Home/
public ActionResult Index()
{
return View();
}
[HttpPost]
public JsonResult getempByNo(int id)
{
List<Employee> emp = new List<Employee>();
emp.Add(new Employee { Id = 1, Date = new DateTime(2019, 02, 12, 13, 23, 00) });
emp.Add(new Employee { Id = 2, Date = new DateTime(2019, 02, 11, 18, 58, 00) });
return Json(emp.Where(x => x.Id == id).FirstOrDefault());
}
}
public class Employee
{
public int Id { get; set; }
public DateTime Date { get; set; }
}
View
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Index</title>
<link href="//netdna.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.5/angular.js"></script>
<script type="text/javascript" src="//angular-ui.github.io/bootstrap/ui-bootstrap-tpls-2.5.0.js"></script>
<script type="text/javascript">
var app = angular.module('MyApp', ['ui.bootstrap'])
app.controller('MyController', function ($scope, $http) {
$scope.Date = new Date();
GetEmployees();
function GetEmployees() {
$scope.employees = [{ Id: 1, Date: '12-02-2019 13:23' },
{ Id: 2, Date: '11-02-2019 18:58'}];
}
function ConvertJsonDateToDateTime(date) {
var parsedDate = new Date(parseInt(date.substr(6)));
var newDate = new Date(parsedDate);
var month = newDate.getMonth() + 1;
var day = newDate.getDate();
var year = newDate.getFullYear();
var hours = newDate.getHours();
var mins = newDate.getMinutes();
return day + "-" + month + "-" + year + " " + hours + ":" + mins;
}
$scope.Edit = function (Id) {
$http({
method: 'post',
url: '/Home/getempByNo',
params: { id: Id }
}).success(function (response) {
$scope.Date = ConvertJsonDateToDateTime(response.Date);
}).error(function (response) {
});
}
});
</script>
</head>
<body>
<div ng-app="MyApp" ng-controller="MyController">
Date TimePicker:
<div uib-timepicker ng-model="Date" hour-step="1" minute-step="1" show-meridian="false">
</div>
<br />
<table class="table table-bordered table-hover table-striped">
<tr class="success">
<th>Id</th>
<th>Date</th>
<th> </th>
</tr>
<tbody ng-repeat="e in employees">
<tr>
<td>{{e.Id}}</td>
<td>{{e.Date}}</td>
<td><a ng-click="Edit(e.Id)" href="">Edit</a></td>
</tr>
</tbody>
</table>
</div>
</body>
</html>
Screenshot
