I have a ViewModel class with a column, named Duration
that contains the amount of hours between two DatetTime
columns (calculated in the query):
public Int32 Duration { get; set; }
I was hoping that I could use an annotation to display the amount of elapsed time, in days and hours (e.g. 0d 15h
).
This doesn't work:
[DisplayFormat(DataFormatString = "{0:dd hh}")]
The logic using a TimeSpan
:
DateTime s;
DateTime.TryParse("2015-03-18 15:00:42.920",out s);
DateTime e;
DateTime.TryParse("2015-03-19 06:52:44.440",out e);
TimeSpan t = e - s;
Console.WriteLine(String.Format("{0}d {1}h", t.Days, t.Hours));
Is there a way to use data annotations (alone) to achieve this?
I decide to add a function to the view:
public string Elapsed(Int32 duration)
{
TimeSpan t = TimeSpan.FromHours(duration);
return String.Format("{0}d {1}h", t.Days, t.Hours);
}
then reference it:
<td> @Elapsed(item.Duration) </td>