0

This is my problem now, i want to get the total hours and mins of work. example from jan. 11 2017 22:00 to Jan. 12 2017 7:00. so far i have it only work when the end date is not change

DateTime pin = today, pout = DateTime.Parse(empTime);
TimeSpan spanMe = pout.Subtract(pin);

spanMe.Hours
spanMe.Minutes

it gives me negative numbers.

Denver
  • 147
  • 3
  • 17
  • 5
    `it gives me negative numbers.` <= That is expected if you subtract a larger item from a smaller item (ie. subtracting a more recent time from an older time). If you always want to see the difference as a positive number and do not want to take into account which is larger then wrap the result of the properties (like .Hours) in `Math.Abs` (absolute value). – Igor Jan 10 '17 at 17:16
  • 3
    You should use the`TotalHours` (or `TotalMinutes`) property – stuartd Jan 10 '17 at 17:16
  • for example jan. 11 2017 @ 7:00 to 22:00 it gives me the correct total number of hours and mins. but from 10pm of jan. 11 to 7am to jan 12 there error come. – Denver Jan 10 '17 at 17:16
  • @stuartd where in the time timespan? – Denver Jan 10 '17 at 17:18
  • [Obligatory link to "time is hard" question.](http://stackoverflow.com/q/6841333/1195056) – krillgar Jan 10 '17 at 17:23
  • im missing the yesterdays time. DateTime Yesterday = pin.AddDays(-1.0); – Denver Jan 10 '17 at 17:44

5 Answers5

3

it gives me negative numbers.

That is expected if you subtract a larger item from a smaller item (ie. subtracting a more recent time from an older time). If you always want to see the difference as a positive number and do not want to take into account which is larger then wrap the result of the properties (like .Hours) in Math.Abs (absolute value).

var hours = System.Math.Abs(spanMe.Hours);
var minutes = System.Math.Abs(spanMe.Minutes);

Also as pointed out by @stuartd there is a difference between Hours/Minutes and TotalHours/TotalMinutes. Make sure you are using the correct one for your needs.

Igor
  • 60,821
  • 10
  • 100
  • 175
  • this gives me 16hrs difference. – Denver Jan 10 '17 at 17:25
  • @Denver - The computer will not lie or make a mistake, if 16 is coming back as `.Hours` in the TimeSpan difference between your dates then there are 16 hours in between. Check your logic and check your actual `DateTime` instances to make sure they actually are what you think/assume they are. – Igor Jan 10 '17 at 17:29
  • @Denver - also check the `DateTime` instances [Kind](https://msdn.microsoft.com/en-us/library/system.datetime.kind(v=vs.110).aspx) component, subtracting an instance with value `DateTimeKind.Local` from `DateTimeKind.Utc` (as example) could produce an unexpected result. – Igor Jan 10 '17 at 17:38
  • thats why it's giving me the 16hrs because it count from 10pm to 7am i miss the yesterdays time which is ive seen it in here http://stackoverflow.com/questions/3023514/getting-the-total-hours-in-a-timespan – Denver Jan 10 '17 at 17:46
  • thank you for the idea. math.abs remove the negative perfectly. ill give this a +1 – Denver Jan 10 '17 at 17:49
1

It should work :

            DateTime pin =  DateTime.Parse("jan 11 2017 22:00");
            DateTime pout = DateTime.Parse("Jan 12 2017 7:00");

            TimeSpan spanMe = pout.Subtract(pin);

            Console.WriteLine("Hours : {0}, Minutes : {1}", spanMe.Hours, spanMe.Minutes);

            Console.ReadLine();
jdweng
  • 33,250
  • 2
  • 15
  • 20
0

if you know what is the latest date, you need arrange it accordingly. If not, you can not multiply by -1:

double GetHouers(DateTime one, DateTime another)
{
    var diff = (one - another).TotalHours;
    return diff > 0 ? diff : diff * -1;
}
dovid
  • 6,354
  • 3
  • 33
  • 73
0

You can subtract one DateTime Object from another, and then use .TotalHours property of the DateTime class to get the number of hours. It will give you a double value representing the total hours.

DateTime pin = today, pout = DateTime.Parse(empTime);
double hours = (pout - pin).TotalHours;
sujith karivelil
  • 28,671
  • 6
  • 55
  • 88
0
DateTime pin = today, pout = DateTime.Parse(empTime);
TimeSpan spanMe = pin.Subtract(pout);

var hours = spanMe.TotalHours;
var minutes = spanMe.TotalMinutes;

You want to use TotalHours and TotalMinutes as these will handle fractions thereof, versus Hours and Minutes which return only whole values. You also need to swap the order of your operands as above for the subtraction step.

CDove
  • 1,940
  • 10
  • 19