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?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You could use
Negate()to change the negative value to positiveFrom MSDN
So you could call the Negate method depending on which value is greater and obtain a positive
TimespanSay we have
startDateandendDate(endDate is greater than startDate), so when we dostartDate.Subtract(endDate)we would get a negativeTimeSpan. 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 TimeSpanEDIT
Please check
Duration()of theTimeSpanthis should give you the absolute value alwaysEarlybeforetime.Duration()