TimeSpan difference from negative value to positive conversion
Asked Answered
C

4

14
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?

Combustor answered 19/1, 2012 at 8:40 Comment(1)
possible duplicate of timespan difference value always positiveAhithophel
A
29

You could use Negate() to change the negative value to positive

From MSDN

If the date and time of the current instance is earlier than value, the method returns a TimeSpan object that represents a negative time span. That is, the value of all of its non-zero properties (such as Days or Ticks) is negative.

So you could call the Negate method depending on which value is greater and obtain a positive Timespan

Say we have startDate and endDate (endDate is greater than startDate), so when we do startDate.Subtract(endDate) we would get a negative TimeSpan. So based on this check you could convert the negative value. So if your outtime is ahead of earlybefore it would give you a negative TimeSpan

EDIT

Please check Duration() of the TimeSpan this should give you the absolute value always

Earlybeforetime.Duration()

Ahithophel answered 19/1, 2012 at 8:47 Comment(5)
i can't understand please explain me or give some example code hereCombustor
sometimes i got positive value also suppose i ll use Negate() means that positive value change to negative.That's no need for me. i just want all return value is positive.Combustor
@user1065029 Expanded a bit more, hope this helps youAhithophel
I have one doubt How can i get the first and last date for a month .. My input is jan or feb . i want the output start date 01-01-2012 and end date 31-01-2012 or start date 01-02-2012 and end date 29-02-2012. How can i get the above output form a monthCombustor
Well i think first day wuld be easy since it would be 1, based on that you can use add days and use DateTime.DaysInMonth and arrive at the last dayAhithophel
H
12

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.

Honghonied answered 19/1, 2012 at 9:4 Comment(0)
G
4
var startTime = new TimeSpan(6, 0, 0); // 6:00 AM
var endTime = new TimeSpan(5, 30, 0); // 5:30 AM 
var hours24 = new TimeSpan(24, 0, 0);
var difference = endTime.Subtract(startTime); // (-00:30:00)
difference = (difference.Duration() != difference) ? hours24.Subtract(difference.Duration()) : difference; // (23:30:00)

can also add difference between the dates if we compare two different dates times the 24 hours new TimeSpan(24 * days, 0, 0)

Greathearted answered 9/10, 2017 at 14:41 Comment(0)
C
1

posativeSpan = new TimeSpan(negativeSpan.Ticks * -1);

Cookstove answered 14/8, 2023 at 7:37 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.