我的模型是
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.
我截取了数据库列的截图。我想在我的Web应用程序中显示StartDate和EndDate之间的小时数差异。我怎样才能做到这一点。我正在使用实体框架。
4 个解决方案
#1
3
You can calculate difference in hour by
您可以按小时计算差异
TimeSpan diff = EndDate - StartDate;
double hours = diff.TotalHours;
and if StartDate
and EndDate
is nullable
then
如果StartDate和EndDate可以为空
TimeSpan? diff = EndDate.Value - StartDate.Value;
double hours = diff.TotalHours;
#2
1
Calculate the difference as:
计算差异为:
var diffHours = (endDateTime- startDateTime).TotalHours
#3
0
Another option can be adding NotMapped
property to calculate it. Your model should look like this:
另一个选项是添加NotMapped属性来计算它。您的模型应如下所示:
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.
从数据库获取数据后,您可以使用HourDifference显示差异。
#4
0
try this.. it will give you differnece of days..
试试这个..它会给你不同的日子..
TimeSpan tSpan = (abc.EndDate.Value).Subtract(abc.StartDate.Value);
var NoOfDays = tSpan.Days;
#1
3
You can calculate difference in hour by
您可以按小时计算差异
TimeSpan diff = EndDate - StartDate;
double hours = diff.TotalHours;
and if StartDate
and EndDate
is nullable
then
如果StartDate和EndDate可以为空
TimeSpan? diff = EndDate.Value - StartDate.Value;
double hours = diff.TotalHours;
#2
1
Calculate the difference as:
计算差异为:
var diffHours = (endDateTime- startDateTime).TotalHours
#3
0
Another option can be adding NotMapped
property to calculate it. Your model should look like this:
另一个选项是添加NotMapped属性来计算它。您的模型应如下所示:
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.
从数据库获取数据后,您可以使用HourDifference显示差异。
#4
0
try this.. it will give you differnece of days..
试试这个..它会给你不同的日子..
TimeSpan tSpan = (abc.EndDate.Value).Subtract(abc.StartDate.Value);
var NoOfDays = tSpan.Days;