Remove seconds from timespan using C #

I want to remove seconds from timespan using c #

My code is here:

TimeSpan lateaftertime = new TimeSpan();
lateaftertime =  lateafter - Convert.ToDateTime(intime) ;

Returns value 00:10:00

But I want to get the following result: 00:10just not a second field :00.

+5
source share
3 answers

Well, you can just do it like

string.Format("{0}:{1}", ts.Hours,ts.Minutes) // it would display 2:5

EDIT

to use its format correctly

string.Format("{0:00}:{1:00}", ts.Hours,ts.Minutes) // it should display 02:05
+13
source

Note that a TimeSpan does not have a format . It is stored in some internal representation, which is not at all like 00:10:00.

hh:mm:ss , TimeSpan String, . , - , - . " " - TimeSpan - TimeSpan.

String, String.Format, V4Vendetta, TimeSpan.ToString ( .NET 4):

string formattedTimespan = ts.ToString("hh\\:mm");
+7
TimeSpan newTimeSpan = new TimeSpan(timeSpan.Hours, timeSpan.Minutes, 0);
+6
source

All Articles