Reputation: 27
public class abc
{
public DateTime StartDate{get;set;}
public DateTime EndDate{get;set;}
}
I have taken screenshot of my database columns. I want to display the difference of hours between StartDate and EndDate into my web application. How can I do that. I am using Entity Framework.
Upvotes: 1
Views: 2389
Reputation: 217
try this.. it will give you differnece of days..
TimeSpan tSpan = (abc.EndDate.Value).Subtract(abc.StartDate.Value);
var NoOfDays = tSpan.Days;
Upvotes: 0
Reputation: 8696
Another option can be adding NotMapped
property to calculate it. Your model should look like this:
public class abc
{
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
[NotMapped]
public double HourDifference
{
get
{
return (EndDate - StartDate).TotalHours;
}
}
}
After getting data from database you can use HourDifference
to display difference.
Upvotes: 0
Reputation: 7352
You can calculate difference in hour by
TimeSpan diff = EndDate - StartDate;
double hours = diff.TotalHours;
and if StartDate
and EndDate
is nullable
then
TimeSpan? diff = EndDate.Value - StartDate.Value;
double hours = diff.TotalHours;
Upvotes: 3
Reputation: 1926
Calculate the difference as:
var diffHours = (endDateTime- startDateTime).TotalHours
Upvotes: 1