Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TimeSpan difference from negative value to positive conversion

Tags:

c#

.net

timespan

TimeSpan Earlybeforetime = new TimeSpan();
Earlybeforetime = earlybefore.Subtract(Convert.ToDateTime(outtime);

Sometimes it returns a negative value. How do I convert the value to be always positive?

like image 663
Romilton Fernando Avatar asked Jan 19 '12 08:01

Romilton Fernando


Video Answer


1 Answers

Negative values are returned when yours Earlybeforetime is earlier that outtime. if you want to have absolute "distance" between two points in time, you can use TimeSpan.Duration method, e.g:

TimeSpan first = TimeSpan.FromDays(5);
TimeSpan second = TimeSpan.FromDays(15);
TimeSpan final = first.Subtract(second).Duration();


Console.WriteLine(final);

this method will return absolute TimeSpan value.

like image 142
Thaven Avatar answered Nov 15 '22 18:11

Thaven