1

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

user123456789
  • 1,914
  • 7
  • 44
  • 100
  • I'm not quite sure if that's a duplicate, but the issue isn't really clear. Maybe you can try to explain it with another example. You can compare timespans which are greater than 24h. – Tim Schmelter Dec 21 '16 at 15:31
  • @TimSchmelter please see my edit – user123456789 Dec 21 '16 at 15:35
  • just do `tmpOverHours = (int)(totalActualHours.TotalHours - int.Parse(txtEstimateHrs.Text))` – Innat3 Dec 21 '16 at 15:35
  • @Innat3 that causes the error `Cannot implicitly convert type 'int' to 'System.TimeSpan'` – user123456789 Dec 21 '16 at 15:37
  • @user123456789 because I understood you only wanted the over hours so I'm outputting them as an integer value instead of a timespan. You can do this then: `tmpOverHours = totalActualHours.Subtract(new TimeSpan(0, int.Parse(txtEstimateHrs.Text), 0, 0, 0));` – Innat3 Dec 21 '16 at 15:40
  • @user123456789 in any case, the problem with your code is that if you parse a single integer value to TimeSpan like that, it will be taken into account as **Days**, so your 10 hours will be parsed as 10 days before being substracted. – Innat3 Dec 21 '16 at 15:53
  • @Innat3 ok so I need parse into hours instead of days. How do I do that? – user123456789 Dec 21 '16 at 15:57
  • @user123456789 I already told you in my previous comment, by using the TimeSpan class constructor properly: `new TimeSpan(0, int.Parse(txtEstimateHrs.Text), 0, 0, 0)` where your textbox's position in the constructor matches the desired time measurement (day, **hour**, min, sec, millisec) – Innat3 Dec 21 '16 at 16:09

3 Answers3

0

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;
        }
NoviceProgrammer
  • 3,347
  • 1
  • 22
  • 32
0

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); 
}
Community
  • 1
  • 1
Innat3
  • 3,561
  • 2
  • 11
  • 29
0

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.

Dbuggy
  • 901
  • 8
  • 16