Click here to Skip to main content
15,906,329 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to find overtime hours of an employee.

offtime should be 5:00PM.(After 5:00PM overtime hours should calculate)

outtime is the time that the employee leave the office.
C#
DateTime offtime = new DateTime();
DateTime outtime = dateTimePicker3.Value;
TimeSpan timeSpan = outtime - offtime;
double totalHours = timeSpan.TotalHours;
textBox4.Text = timeSpan.ToString();


I want to find the overtime hours.
please help me!

I'm looking for your answers .
Posted
Updated 11-Oct-13 10:46am
v3

You already have a correct code. The only problem is that offtime is assigned to the "default" time. Probably, you need to subtract two times with the same date, so the date would be taken out of equation. I don't know where you want to get another time, but one comes from your DateTimePicker. Copy it, and extract the date from it, with "zero" time part:
http://msdn.microsoft.com/en-us/library/system.datetime.date.aspx[^].

Then, add required time to the copy. Assuming you know that time with the accuracy of seconds, use this:
http://msdn.microsoft.com/en-us/library/system.datetime.addseconds.aspx[^].

You got two points of time, one from the picker, another from your known time. Subtract it and proceed the way you already did.

—SA
 
Share this answer
 
Hi,
You can use below code.

DateTime offtime = DateTime.Today.AddHours(1);
DateTime outtime = dateTimePicker1.Value;
TimeSpan totalhours = outtime.Subtract(offtime);
if (totalhours > TimeSpan.Zero)
    textBox4.Text = totalhours.ToString();
else
    textBox4.Text = TimeSpan.Zero.ToString();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900