Hi mahesh213,
Refer below sample code and modify as per your need.
Model
public class EmployeeModel
{
public int Id { get; set; }
public string Name { get; set; }
public string Date { get; set; }
public string Active { get; set; }
}
Controller
public class HomeController : Controller
{
// GET: Home
public ActionResult Index()
{
return View();
}
public JsonResult GetAll()
{
List<EmployeeModel> employees = new List<EmployeeModel>();
employees.Add(new EmployeeModel { Id = 1, Name = "a", Date = "1/5/2020" });
employees.Add(new EmployeeModel { Id = 2, Name = "b", Date = "1/4/2020" });
employees.Add(new EmployeeModel { Id = 3, Name = "c", Date = "1/4/2019" });
employees.Add(new EmployeeModel { Id = 4, Name = "d", Date = "13/5/2020 12:50:12" });
foreach (EmployeeModel employee in employees)
{
employee.Active = CalCulateTime(Convert.ToDateTime(employee.Date));
}
return Json(employees, JsonRequestBehavior.AllowGet);
}
public string CalCulateTime(DateTime date)
{
string message = "";
DateTime currentDate = DateTime.Now;
TimeSpan timegap = currentDate - date;
message = string.Concat(date.ToString("MMMM dd, yyyy"), " at ", date.ToString("hh:mm tt"));
if (timegap.Days > 365)
{
message = string.Concat((((timegap.Days) / 30) / 12), " years/s ago");
}
else if (timegap.Days > 31)
{
message = string.Concat(((timegap.Days) / 30), " month/s ago");
}
else if (timegap.Days > 1)
{
message = string.Concat(timegap.Days, " day/s ago");
}
else if (timegap.Days == 1)
{
message = "Posted yesterday";
}
else if (timegap.Hours >= 2)
{
message = string.Concat(timegap.Hours, " hour/s ago");
}
else if (timegap.Hours >= 1)
{
message = "an hour ago";
}
else if (timegap.Minutes >= 60)
{
message = "more than an hour ago";
}
else if (timegap.Minutes >= 5)
{
message = string.Concat(timegap.Minutes, " minute/s ago");
}
else if (timegap.Minutes >= 1)
{
message = "a few minute/s ago";
}
else
{
message = "less than a minute ago";
}
return message;
}
}
View
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css" />
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular.min.js"></script>
<script type="text/javascript">
var app = angular.module('MyApp', [])
app.controller('MyController', function ($scope, $http) {
GetEmployees();
function GetEmployees() {
$scope.employees = [];
$http({
method: 'Get',
url: '/Home/GetAll'
}).success(function (data, status, headers, config) {
$scope.employees = data;
}).error(function (data, status, headers, config) {
$scope.message = 'Unexpected Error';
});
}
});
</script>
</head>
<body>
<div ng-app="MyApp" ng-controller="MyController" align="center">
<table class="table table-bordered table-hover table-striped">
<tr>
<th>Id</th>
<th>Name</th>
<th>Active</th>
</tr>
<tbody ng-repeat="e in employees">
<tr>
<td>{{e.Id}}</td>
<td>{{e.Name }}</td>
<td>{{e.Active}}</td>
</tr>
</tbody>
</table>
</div>
</body>
</html>
Screenshot