Formatting a TimeSpan String

The code has:

gvRankings.DataSource = rankings.OrderBy(rg => rg.Swimtime).Take(100).ToArray();  
gvRankings.DataBind();  

(Swimtime is here TimeSpan)

At the front, I

< asp:TemplateField HeaderText="Tijd" ItemStyle-CssClass="time" HeaderStyle-CssClass="smallheader">
< ItemTemplate><%# ("Swimtime")%>< /ItemTemplate>
< /asp:TemplateField>

I would like to format the swimming time as hh:mm:dd.ff, but I (after 2 hours of trying) absolutely do not know how to do this .....

+3
source share
2 answers

string.Format("{0:hh\\:mm\\:dd\\.ff}", yourTimeSpan)

+4
source

You can trick and use the property Ticksto create DateTimeand the format you want:

string formatedTime = new DateTime(swimTime.Ticks).ToString("hh:mm:dd.ff");
+4
source

Source: https://habr.com/ru/post/1793164/


All Articles