working with date calculations

SDR

Member
Joined
Oct 3, 2008
Messages
9
Programming Experience
Beginner
I am trying to calculate hour worked based on a start date and an end date that the user picks. If the dates are on the same day I have no problem getting the hours but I am having trouble with it if they pick multiple days. I need it to return 8 hours if the person was off for the whole day but I am getting back 24 hours if they are off for 2 days, here is how I am attempting to solve this now, any help would be appreciated. I know this is a mess that is why I am asking for some help. :eek:

VB.NET:
'--Loop through every date in range provided by user
        While curDate <= Me.dtpEndDate.Value.Date
            '--Do NOT count Sunday, Saturday or Holidays, they are not work days.
            If curDate.DayOfWeek <> DayOfWeek.Saturday And curDate.DayOfWeek <> DayOfWeek.Sunday _
                And HolidayChecker(curDate) = False Then
                tempNewHrs = 0
                startTime = Me.dtpStartDate.Value.TimeOfDay.ToString() 'Set current time
                endTime = Me.dtpEndDate.Value.TimeOfDay.ToString() 'Set current time
                If Me.dtpEndDate.Value.Date = Me.dtpStartDate.Value.Date Then
                    'tempNewHrs = 0
                    tempNewHrs = tempNewHrs + DateDiff(DateInterval.Minute, Me.dtpStartDate.Value, Me.dtpEndDate.Value)
                    tempTotalHrs = tempTotalHrs + tempNewHrs
                ElseIf Me.dtpEndDate.Value.Date <> curDate Or Me.dtpStartDate.Value.Date <> curDate Then
                    tempNewHrs = tempNewHrs + 8
                    tempTotalHrs = tempTotalHrs + tempNewHrs


                ElseIf Me.dtpStartDate.Value.TimeOfDay.ToString() > tempStartTimeString Then
                    tempNewHrs = DateDiff(DateInterval.Minute, Me.dtpStartDate.Value, tempEndTimeString)
                End If
            End If

 '--Increment to next day
                    curDate = curDate.AddDays(1)
        End While
 
It would help if you would supply your problem code where it can be copied and run as is.

Don't you want to use DateInterval.Hour here?
VB.NET:
tempNewHrs = tempNewHrs + DateDiff(DateInterval.Minute, Me.dtpStartDate.Value, Me.dtpEndDate.Value)

Try calculating your same day or start and end days separately for the time parts, then where they are greater than one day apart try something like:

'Note you can take advantage of DayOfWeek being an integer where 0 = Monday and 6 = Saturday as shown

VB.NET:
Dim curDate As Date = dtpStartDate.Value.AddDays(1).Date
      Do While curDate < dtpEndDate.Value.AddDays(-1).Date
         If curDate.DayOfWeek < DayOfWeek.Saturday And Not HolidayChecker(curDate) Then
            TotHrs += 8
         End If
      Loop
 
Thanks for the reply. I am using minutes because it comes down to hours and minutes in the calculations a lot of the time. The code I provided is what I have developed so far, not that there is a problem with it but more that I am just stuck in trying to figure out how to account for total number of hours the person takes off. For example...

A person clocks out at 10:45 AM on Tuesday and clocks back in at 1:00 PM on Friday then I need to filter out the extra hours to come up with the total time that he was off in working hours.
 
I think I have finally worked it out.

VB.NET:
Dim curDate As Date
        Dim tempStartTimeString As Date
        Dim tempEndTimeString As Date
        Dim tempTotalHrs As Integer
        Dim tempNewHrs As Integer
        curDate = Me.dtpStartDate.Value.Date    'Set current date to start date provided by user
        tempEndTimeString = "#" & curDate.Month.ToString().TrimEnd("#") & "/" & curDate.Day.ToString().TrimEnd("#") & "/" & curDate.Year.ToString().TrimEnd("#") & " 04:30:00 PM#"
        tempStartTimeString = "#" & Me.dtpStartDate.Value.Month.ToString().TrimEnd("#") & "/" & Me.dtpStartDate.Value.Day.ToString().TrimEnd("#") & "/" _
        & Me.dtpStartDate.Value.Year.ToString().TrimEnd("#") & " 08:00:00 AM#"

        '--Loop through every date in range provided by user
        While curDate <= Me.dtpEndDate.Value.Date
            '--Do NOT count Sunday, Saturday or Holidays, they are not work days.
            If curDate.DayOfWeek <> DayOfWeek.Saturday And curDate.DayOfWeek <> DayOfWeek.Sunday _
                And HolidayChecker(curDate) = False Then
                tempNewHrs = 0 'resets the hours for the current day
                
                'Do when start dates and end dates match
                If Me.dtpEndDate.Value.Date = Me.dtpStartDate.Value.Date Then
                    'Gets the difference in minutes between dtpStartDate and dtpEndDate
                    tempNewHrs = tempNewHrs + DateDiff(DateInterval.Minute, Me.dtpStartDate.Value, Me.dtpEndDate.Value)
                    tempTotalHrs = tempTotalHrs + tempNewHrs 'Adds the new minutes to the running total held in tempTotalHrs
                    'Do when dtpEndDate is after dtpStartDate
                ElseIf Me.dtpEndDate.Value.Date <> curDate And Me.dtpStartDate.Value.Date = curDate Then
                    tempNewHrs = tempNewHrs + DateDiff(DateInterval.Minute, Me.dtpStartDate.Value, tempEndTimeString)
                    tempTotalHrs = tempTotalHrs + tempNewHrs 'Adds the new minutes to the running total held in tempTotalHrs
                    'Do when dtpEndDate matches the current day but dtpStartDate does NOT match the current day
                ElseIf Me.dtpEndDate.Value.Date = curDate And Me.dtpStartDate.Value.Date <> curDate Then
                    tempNewHrs = tempNewHrs + DateDiff(DateInterval.Minute, Me.dtpStartDate.Value, tempStartTimeString)
                    tempTotalHrs = tempTotalHrs + tempNewHrs 'Adds the new minutes to the running total held in tempTotalHrs
                    'Do when dtpEndDate and dtpStartDate do not match the current day
                ElseIf Me.dtpStartDate.Value.Date <> curDate And Me.dtpEndDate.Value.Date <> curDate Then
                    tempNewHrs = tempNewHrs + 480 'Adds 480 minutes(8 hours)
                    tempTotalHrs = tempTotalHrs + tempNewHrs 'Adds the new minutes to the running total held in tempTotalHrs
                End If
            End If

            Me.lblTotalNumOfHrs.Text = CDec(tempTotalHrs.ToString() / 60)

                    '--Increment to next day
                    curDate = curDate.AddDays(1)
        End While
 
Last edited by a moderator:
Back
Top