Time Difference Calculation In C#
-
Hi To All, Please help me to solve my problem. I am A Window Application developer. I am Developing a Attendance Calculation Software. I have InTime And OutTime of Employee's. Every Thing is Working fine. The Only thing That I Cannot Calculate is OT i.e. OverTime of Employee. I am not getting The exact Result. I have shift time like 9:00 AM To 6:00 PM. I am Unable to calculate the OT if Employees Shift Ends On Next Day. For Example : If Shift End Time Is 6:00 PM And Employee Shift Ends At 12:00 AM Or After 12:00 AM Then It Gives Me Incorrect Answer. I am Unable to Resolve This Problem. Please Help Me. Thanks & Regards Sanket Patil.
.
-
Hi To All, Please help me to solve my problem. I am A Window Application developer. I am Developing a Attendance Calculation Software. I have InTime And OutTime of Employee's. Every Thing is Working fine. The Only thing That I Cannot Calculate is OT i.e. OverTime of Employee. I am not getting The exact Result. I have shift time like 9:00 AM To 6:00 PM. I am Unable to calculate the OT if Employees Shift Ends On Next Day. For Example : If Shift End Time Is 6:00 PM And Employee Shift Ends At 12:00 AM Or After 12:00 AM Then It Gives Me Incorrect Answer. I am Unable to Resolve This Problem. Please Help Me. Thanks & Regards Sanket Patil.
.
Too easy. Look at the DateTime and TimeSpan classes. Use something like
DateTime start = new DateTime(... ... ...);
DateTime finish = new DateTime(... ... ...);
TimeSpan worked = finish - start;then pull
worked
apart as you need.Software rusts. Simon Stephenson, ca 1994.
-
Too easy. Look at the DateTime and TimeSpan classes. Use something like
DateTime start = new DateTime(... ... ...);
DateTime finish = new DateTime(... ... ...);
TimeSpan worked = finish - start;then pull
worked
apart as you need.Software rusts. Simon Stephenson, ca 1994.
Hi Perter_in_2780, Thanks for Your Reply. But Still I am getting Incorrect Result. I have only Time. When I convert the Out Time in datetime Then Both the Time that is employees out time and shift out time has default date as todays date and if the Emd Time Of Employee is 12:00 AM and Shift Out Time ois 6:00 PM Then Iam Getting the Result is 17.00 in TimeSpan. Which I dont want. I want Result as 6:00. Iam unable to calculate time difference in case when time is from PM to AM. Thanks & Regards Sanket Patil
.
-
Hi Perter_in_2780, Thanks for Your Reply. But Still I am getting Incorrect Result. I have only Time. When I convert the Out Time in datetime Then Both the Time that is employees out time and shift out time has default date as todays date and if the Emd Time Of Employee is 12:00 AM and Shift Out Time ois 6:00 PM Then Iam Getting the Result is 17.00 in TimeSpan. Which I dont want. I want Result as 6:00. Iam unable to calculate time difference in case when time is from PM to AM. Thanks & Regards Sanket Patil
.
You need to include at least part of the date in start and end DateTime's. The dates can be "made up", like this:
DateTime start = new DateTime(2000, 1, 1, 18, 0, 0); //6pm "yesterday"
DateTime finish = new DateTime(2000, 1, 2, 9, 0, 0); //9am "today"
TimeSpan worked = finish - start;
...That should come out at 15 hours. Peter
Software rusts. Simon Stephenson, ca 1994.
-
Hi To All, Please help me to solve my problem. I am A Window Application developer. I am Developing a Attendance Calculation Software. I have InTime And OutTime of Employee's. Every Thing is Working fine. The Only thing That I Cannot Calculate is OT i.e. OverTime of Employee. I am not getting The exact Result. I have shift time like 9:00 AM To 6:00 PM. I am Unable to calculate the OT if Employees Shift Ends On Next Day. For Example : If Shift End Time Is 6:00 PM And Employee Shift Ends At 12:00 AM Or After 12:00 AM Then It Gives Me Incorrect Answer. I am Unable to Resolve This Problem. Please Help Me. Thanks & Regards Sanket Patil.
.
-
The funniest thing about this particular signature is that by the time you realise it doesn't say anything it's too late to stop reading it. My latest tip/trick Visit the Hindi forum here.
Hi Abhinav s, Thanks for your reply But I am Not Getting The Solution if its night shift. Thanks And Regards Sanket Patil
.
-
Hi To All, Please help me to solve my problem. I am A Window Application developer. I am Developing a Attendance Calculation Software. I have InTime And OutTime of Employee's. Every Thing is Working fine. The Only thing That I Cannot Calculate is OT i.e. OverTime of Employee. I am not getting The exact Result. I have shift time like 9:00 AM To 6:00 PM. I am Unable to calculate the OT if Employees Shift Ends On Next Day. For Example : If Shift End Time Is 6:00 PM And Employee Shift Ends At 12:00 AM Or After 12:00 AM Then It Gives Me Incorrect Answer. I am Unable to Resolve This Problem. Please Help Me. Thanks & Regards Sanket Patil.
.
For the night shift split the calc in 2 start to 24:00, 00:00 to finish. Decide which calc to use based on the start time or if the end time is less than the start time. Os as others have suggested add the date to the start/end variables. Bloody hell this is simple stuff, try thinking.
Never underestimate the power of human stupidity RAH
-
Hi To All, Please help me to solve my problem. I am A Window Application developer. I am Developing a Attendance Calculation Software. I have InTime And OutTime of Employee's. Every Thing is Working fine. The Only thing That I Cannot Calculate is OT i.e. OverTime of Employee. I am not getting The exact Result. I have shift time like 9:00 AM To 6:00 PM. I am Unable to calculate the OT if Employees Shift Ends On Next Day. For Example : If Shift End Time Is 6:00 PM And Employee Shift Ends At 12:00 AM Or After 12:00 AM Then It Gives Me Incorrect Answer. I am Unable to Resolve This Problem. Please Help Me. Thanks & Regards Sanket Patil.
.
This is exactly why you store start and end date and not just time. If you stored: start date = 2010-09-12 20:00:00 end date = 2010-09-13 07:00:00 then you would have no problem. Go back and fix your model/database before you continue with incorrect data storage.
-
Hi To All, Please help me to solve my problem. I am A Window Application developer. I am Developing a Attendance Calculation Software. I have InTime And OutTime of Employee's. Every Thing is Working fine. The Only thing That I Cannot Calculate is OT i.e. OverTime of Employee. I am not getting The exact Result. I have shift time like 9:00 AM To 6:00 PM. I am Unable to calculate the OT if Employees Shift Ends On Next Day. For Example : If Shift End Time Is 6:00 PM And Employee Shift Ends At 12:00 AM Or After 12:00 AM Then It Gives Me Incorrect Answer. I am Unable to Resolve This Problem. Please Help Me. Thanks & Regards Sanket Patil.
.
Hi, there are only two cases that could work well: 1. you have date AND time information, not just time. Then a TimeSpan calculated as the difference of two DateTime values is perfect. 2. you only have time information, but you also know the context enforces something. In your case it would be: the time worked is anywhere between 0 and 24 hours. So, calculate the difference in any way you see fit (even with TimeSpan=DateTime-DateTime where both DateTimes have the same or no date), then enforce your boundary conditions: if the TimeSpan is less than zero, add 24 hours. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.