Comparing times when over 24 hours

1.2k views Asked by At

I have a TimeSpan field that adds up time spent on something. For example the time could be 33 hours, so the format is 33:56:00

I want to compare this to 10 hours to calculate how many over hours were done.

TimeSpan totalActualHours = new TimeSpan(0, 0, 0, 0, 0);
if (totalActualHours > TimeSpan.Parse(txtEstimateHrs.Text))
{
     tmpOverHours = totalActualHours.Subtract(TimeSpan.Parse(txtEstimateHrs.Text));
} 

But since totalActualHours is over 24 hours the format is coming out like 1.09:56:00 instead of 33:56:00. So txtEstimateHrs.Text is equal to 10 and I want to see if 33:56:00 is greater and if so then how many hours is it greater?

So the code is comparing if (1.09:56:00 > 10.00:00:00) so it never goes into the if statement.

The issue here is Timespan in converting the hours into days so 33 hours changes to 1 day and 9 hours, and the txtEstimateHrs.Text is an integar 10 and that changes to 10 days. I need both times to be in hours format and be able to compare them

3

There are 3 answers

0
Innat3 On

You just need to properly construct the timespan object using the appropiate format. In your case, you can choose between

hour, min sec

day, hour, min, sec, millisec

Sample code:

Case 1

TimeSpan tmpOverHours;
TimeSpan totalActualHours = new TimeSpan(33, 56, 0);
TimeSpan hoursToCompare = new TimeSpan(int.Parse(txtEstimateHrs.Text), 0, 0);

if (totalActualHours > hoursToCompare)
{
    tmpOverHours = totalActualHours.Subtract(hoursToCompare); 
}

Case 2

TimeSpan tmpOverHours;
TimeSpan totalActualHours = new TimeSpan(0, 33, 56, 0, 0);
TimeSpan hoursToCompare = new TimeSpan(0, int.Parse(txtEstimateHrs.Text), 0, 0, 0);

if (totalActualHours > hoursToCompare)
{
    tmpOverHours = totalActualHours.Subtract(hoursToCompare); 
}
0
Dbuggy On

It seems you are having a parsing error when you are doing:

TimeSpan.Parse(txtEstimateHrs.Text)

if the text is "10" the parse method will interpret the value as days.

So you could change that code to something like:

TimeSpan.FromHours(int.Parse(txtEstimateHrs.Text))

Which will parse the number in the textbox into an int and use that to create a TimeSpan which correctly has the number of hours and not days.

Edit: On a side note, don't parse the text twice, better use a variable to hold the parsed TimeSpan and then use it.

2
NoviceProgrammer On

I am not sure i understood your requirement but you can use the TimeSpan.Compare() method.

var t1 = new TimeSpan(33, 21, 12);
var t2 = new TimeSpan(10, 0, 0);

if (TimeSpan.Compare(t1, t2) > 0)
{
    Console.WriteLine(t1.ToString() + " is longer");
}

Edit:

The above code will work fine if the Timespan objects can be created correctly. In case you are working with strings in the format of hh:mm:ss then you will need to split them and call the correct Timespan constructor. Something like below:

public static TimeSpan ConvertStringToTimeStamp(string s)
        {
            // add checks for input like >0, not null or empty

            var split = s.Split(':');
            TimeSpan ts;

            switch (split.Length)
            {
                case 3:
                    ts = new TimeSpan(int.Parse(split[0]),    // hours
                                       int.Parse(split[1]),   // minutes
                                       int.Parse(split[2]));  // seconds                            // seconds);
                    break;
                case 2:
                    ts = new TimeSpan(int.Parse(split[0]),    // hours
                                       int.Parse(split[1]),    // minutes
                                       0);                     // 0 seconds
                    break;
                case 1:
                    ts = new TimeSpan(int.Parse(split[0]),    // hours
                                       0,                     // 0 minutes
                                       0);                    // 0 seconds
                    break;
                default:
                    throw new Exception("Invalid Input");

            }

            return ts;
        }