Rupert
Rupert

Reputation: 4347

Display SQL Data Type Time as Standard Time

My Database stores time in the SQL DataType Time. I'm using mvc 3 in vb.net and trying to get the time to display in standard format (AM/PM). But, I keep getting an error "Unable to cast object of type 'System.TimeSpan' to type 'System.IConvertible'."

This is the code I'm using

<h4>Tuesday</h4>
<ul>
@For Each c As ClassSchedule In ViewBag.Tuesday

   @<li>@c.timeStart - @c.timeEnd - @c.name</li>

Next
</ul>

Any help is appreciated.

Upvotes: 1

Views: 657

Answers (3)

Morteza
Morteza

Reputation: 2428

Edit:

@<li>
  @(new DateTime(c.timeStart.Ticks).ToString("hh:mm:ss tt")) - 
  @(new DateTime(c.timeEnd.Ticks).ToString("hh:mm:ss tt")) - @c.name
</li> 

Eror Occured becuse you subtract Dates

 @<li>
   <span>@c.timeStart </span>- 
   <span>@c.timeEnd </span>- 
   <span>@c.name</span>
  </li>

Upvotes: 1

Rupert
Rupert

Reputation: 4347

Figured it out --- you have to convert the time into a string first

@

  • @Convert.ToDateTime(c.timeStart.ToString).ToShortTimeString - @Convert.ToDateTime(c.timeEnd.ToString).ToShortTimeString - @c.name
  • Upvotes: 0

    Janus Troelsen
    Janus Troelsen

    Reputation: 21270

    You can explicitly call ToString() on your TimeSpan.

    Upvotes: 0

    Related Questions