0

I have DisplayFor showing a TimeSpan-value (Duration) in this format: 35.04:08:43.2470000 (dd.HH:mm:ss.fffffff).

My goal is a human-readable string "HH:mm" so I followed How to display a TimeSpan in MVC Razor and tried making a DisplayTemplate in ~views/Shared/TimeSpanTemplate.cshtml:

@model TimeSpan
@{
    TimeSpan initialValue = Model;
}
@string.Format("{0}:{1}", (initialValue.Days * 24) + initialValue.Hours, initialValue.Minutes)

@Scripts.Render("~/bundles/bootstrap")

This is my model:

[DisplayFormat(DataFormatString = "{0:HH:mm}", ApplyFormatInEditMode = true)]
[DisplayName("Varighed")]
public virtual TimeSpan? Duration
{
   get {
      return SolvedDate.HasValue ? (TimeSpan?)SolvedDate.Value.Subtract(ReportedDate) : null;        }
}

This is my view:

<td>
    @Html.DisplayFor(modelItem => item.Ticket.Duration)
</td>

I get this compile formatexception error:

Input string was not in a correct format. --> "@Html.DisplayFor(modelItem => item.Ticket.Duration)"

System.FormatException was unhandled by user code
HResult=-2146233033 Message=Input string was not in a correct format. Source=mscorlib

I have successfully created DateTime-templates for EditViews, but I cant seem to get around this problem on DisplayFor with TimeSpan-types.

I dont have the faintest idea what I am doing, so any clues are appreciated! :) Thanks in advance!

Community
  • 1
  • 1
F. B. R.
  • 23
  • 1
  • 6
  • If you make your `Duration` property a `TimeSpan` (rather than `TimeSpan?`) does your problem go away? I suspect this is because your template is declared for `TimeSpan` not `Nullable`. – CodingGorilla Mar 17 '16 at 15:08
  • I think you are right that the nullable part might be a part of the problem.I need the nullable timespan as some records in my database do not have a duration yet. – F. B. R. Mar 17 '16 at 15:53
  • Shouldn't you be getting the Ticket from `modelItem` and not `item`? `@Html.DisplayFor(modelItem => modelItem.Ticket.Duration)` – Andy T Mar 17 '16 at 17:25
  • It is not really relevant to my question, but no - my foreach loop states '@foreach (var item in Model) { }' – F. B. R. Mar 18 '16 at 09:03
  • My main question here is - how do I transform my Datetime into a string from my view template without having the format exception error? :) – F. B. R. Mar 18 '16 at 09:22
  • to change dateTime format you can just do this `@Convert.ToDateTime(item.ItemName).ToString("dd-MM-yyyy")` – FllnAngl Mar 15 '18 at 11:04

3 Answers3

1

After reading How can I String.Format a TimeSpan object with a custom format in .NET?

I managed to solve this myself, by outcommenting my TimeSpanTemplate, and simply edit this line in my model from: [DisplayFormat(DataFormatString = "{0:HH:mm}", ApplyFormatInEditMode = true)]

to

[DisplayFormat(DataFormatString = "{0:%d}d {0:%h}h {0:%m}m {0:%s}s", ApplyFormatInEditMode = true)]

Community
  • 1
  • 1
F. B. R.
  • 23
  • 1
  • 6
0

This is not a direct answer to solve this problem, but I think it would be better to use the Humanizer library.

You would be able to do the following:

SolvedDate.Humanize();

See here: https://github.com/Humanizr/Humanizer#humanize-timespan

Jamie Rees
  • 7,973
  • 2
  • 45
  • 83
  • I dont really feel like introducing a whole new library to fix this, as it should be a little less complicated to fix.. I think I am missing something simple here.. :) – F. B. R. Mar 17 '16 at 15:50
0

Your Timespan is nullable so try:

<td>
    @Html.Label("MyTimeSpan", Model.Ticket.Duration != null ? Model.Ticket.Duration.Value : string.Empty)
</td>
James Dev
  • 2,979
  • 1
  • 11
  • 16
  • I dont think I can just change the DisplayFor to a lable, and I cant figure out the syntax I should use, to get the linq-query modelItem => item.Ticket.Duration into your line.. – F. B. R. Mar 17 '16 at 15:52