Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

timespan difference value always positive

Tags:

c#

timespan

i want to convert the timespan diff value always positive

My code is here :

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

i want to get the result of lateaftertime always positive.. please help me..

like image 323
Romilton Fernando Avatar asked Jan 19 '12 07:01

Romilton Fernando


People also ask

How do we determine a TimeSpan?

A TimeSpan object represents a time interval (duration of time or elapsed time) that is measured as a positive or negative number of days, hours, minutes, seconds, and fractions of a second. The TimeSpan structure can also be used to represent the time of day, but only if the time is unrelated to a particular date.

What is TimeSpan?

/ˈtaɪm.spæn/ a period of time within which something happens, or between two events: The attacks all happened less than 5 miles apart within a three-week time span.

What is the default value for TimeSpan in C#?

The default value is TimeSpan(30, 23, 59, 59) .


2 Answers

You can use lateaftertime.Duration() to get a non-negative span.

like image 189
recursive Avatar answered Sep 21 '22 16:09

recursive


You could use Math.Abs():

        lateaftertime = new TimeSpan(Math.Abs(lateaftertime.Ticks)); 

User V4Vendetta made the right call in a comment though. Use the TimeSpan.Duration property, it always returns the absolute value.

like image 22
Hans Passant Avatar answered Sep 21 '22 16:09

Hans Passant