-3

Could you please help me? I'm trying to create an attendance system wherein the undertime and overtime will be automatically computed, but there's an error. For example the employee's scheduled out is 11 PM, and their overtime 12 AM which is 1 hour, but the output is 23. Can anyone help me how to compute elapsed time?

string datetime = "12:00 AM"; // this is the time you are going to time out
string OUT = "11:00 PM"; // this is your scheduled out
DateTime d1 = Convert.ToDateTime(datetime);
DateTime d2 = Convert.ToDateTime(OUT); 
TimeSpan ts = d2.Subtract(d1);

MessageBox.Show(ts.TotalHours.ToString()); // output is 23 which is wrong, it must be 1 hour overtime only
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Manjay
  • 39
  • 1
  • 10
  • For those wishing to assist here, please read https://stackoverflow.com/questions/54141322/calculate-undertime-and-overtime-automatically#comment95113971_54141322 beforehand. The OP is asking the same question repeatedly. – mjwills Jan 11 '19 at 06:58

3 Answers3

2

IMO, in order to fix your problem, you have to work with the actual datetime objects. Always record actual system datetime without manipulating parts of it. For your case you should have the fields for record scheduled_work_start, scheduled_work_finished and actual_work_finished for an instance, say one of the employees starts work at 10-01-2019 14:00:00 (2 PM) and finishes her time at 10-01-2019 23:00:00 (11 PM) and assume she did one hour overtime. The system should record the actual_work_finished time as 11-01-2019 00:00:00 (12 AM) When you require to calculate or find out the extra time calculate:

var over_time_in_hours = actual_work_finished.Substract(scheduled_work_finished).TotalHours;

Hope this makes sense.

RSF
  • 510
  • 6
  • 18
  • thankyou, yes you are right. the dates between hours must included to determine whether it is elapsed or not, thankyou so much ! you are great. now i can finish my develop system – Manjay Jan 10 '19 at 03:07
  • Happy to hear my answer helped you to resolve your issue. Cheers! – RSF Jan 10 '19 at 03:34
  • It would be nice if you could translate your question into code. So please let me know the values of: `scheduled_work_start`, `scheduled_work_finished ` and `actual_work_finished` Then we can come up with a solution to your problem. – RSF Jan 10 '19 at 10:34
0

If you print your d1 and d2 times:

d1 time => "09.01.2019 00:00:00". 
d2 time => "09.01.2019 23:00:00".

Then 23-0 = 23 is the expected result.

By the way you can achieve your result by adding 1 day to d1 time object and subtract this result from d2 object:

TimeSpan ts = d1.AddDays(1).Subtract(d2);
Console.WriteLine(ts.TotalHours.ToString());
Serhat Oz
  • 788
  • 8
  • 12
  • Yes it gives me a correct answer. thankyou, but the problem is that if you set the time in 12PM and 1PM, it gives me 23 which is wrong. how about this? – Manjay Jan 09 '19 at 07:28
0

Let's start by naming your variables something that helps us to reason about the code.

// this is the time you are going to time out
DateTime actual = Convert.ToDateTime("12:00 AM");

// this is your scheduled out
DateTime scheduled = Convert.ToDateTime("11:00 PM"); 

TimeSpan overtime = scheduled.Subtract(actual);

What we find is that you're performing the wrong calculation to start with. This would be the correct calculation:

TimeSpan overtime = actual.Subtract(scheduled);

When we do that though we are now getting -23 hours. This is because your actual time isn't after your scheduled time. For that you need to add a day.

Try this:

// this is the time you are going to time out
DateTime actual = Convert.ToDateTime("12:00 AM").AddDays(1);

// this is your scheduled out
DateTime scheduled = Convert.ToDateTime("11:00 PM"); 

TimeSpan overtime = actual.Subtract(scheduled);

Then you get the result that you want - i.e. 1 hour.

Enigmativity
  • 113,464
  • 11
  • 89
  • 172
  • yes i have an issue with 12AM and 12PM sir. i just wanted to automatically compute the undertime and overtime of the employee but there is something wrong with computation. i cant get it – Manjay Jan 09 '19 at 10:40
  • 1
    @Manjay Provide sample data that consists of datetime, OUT and your expected hour difference result – Serhat Oz Jan 09 '19 at 10:47
  • datetime => "12:00 AM" OUT => "11:00 PM" expected hour => 1. Give at least 5 example like this. Especiallly problematic cases – Serhat Oz Jan 09 '19 at 10:55
  • 1
    @Manjay - Please do not post screen shots unless you have posted your 5+ examples as text. We need inputs and expected outputs please. 5+ examples. – Enigmativity Jan 10 '19 at 03:09