Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Dear all
Iam in a HR project and I need to define Shift for Employees
Shift had two feilds for punchcards reading , Swipein and swipeOut . I use two datetimepicker with format as time and showupdown as true to set the swipe in and out time
and read like this
C#
public void calculateduration()
    {
        TimeSpan swipein = dtp_toTime.Value.TimeOfDay;
        TimeSpan swipeout = dtp_FromTime.Value.TimeOfDay;
        TimeSpan duration = dtp_toTime.Value.TimeOfDay - dtp_FromTime.Value.TimeOfDay;
        MessageBox.Show(duration.ToString());
       
    }


in my SQL server database I had all these three feilds swipein ,swipeout,duration as Time datatype ,and I want to insert these values there

For testing I give four values

Swipein Swipeout Duration(output displayed)
8 am 4 pm 8 hrs >>>>its same day sday shift

3 pm 10 pm 8 hrs >> same day daynight shifty

10 pm 6 am(next day) -16 hrs >>> the prblm..8 must come here

Any Idea pls
or can any one tell me an idea to check whether the duration recieved is negative like
C#
if(duration <0)
{
duration= 24-duration;
} 
Posted

1 solution

Instead of using time data type why not use datetime to store the date and the time all together. This way the interval would always be correct even if the date changes.

Another thing is that is it really needed to store the duration. The duration can always be calculated and it's not advisable to store calculated data in this kind of system. If you want you can create a computed column in the table or calculate the duration in SQL statements or at client side.
 
Share this answer
 
Comments
SREENATH GANGA 2-Sep-12 7:36am    
I was also wondering whether to use a datetime or Time ,,but I didnt want the date part t also to go to database ,this is the flace where i define the Shift time it doesnot need a date it just says shift A is from this time to this thime evaery day ..I will be using this time for every swipe in machine ...anyway was a good idea forwarded by you
Wendelius 2-Sep-12 10:02am    
Ok, so you just need the shifts. Perhaps then the other way around. Store the time and the duration and don't store the ending time. Now you would have the proper data for each day. Just pick a date, add the time and you have a starting point. Next add the duration and you have the ending point. When you add the duration to a datetime, the calculation would go correctly.

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