如何计算一定范围内的小时数?

时间:2017-02-21 22:36:34

标签: ms-access ms-access-2016

我有两个字段,开始时间和结束时间。如果两者之间的跨度超过一个日历日,它们只包含时间或日期和时间。

我试图找出从下午6点到早上6点之间的开始和结束之间的小时数。

目前我有以下内容,如果time_finished在同一天,我会得到几个小时,但我正在努力解决其他案例的问题。

IIf(TimeValue([time_finish])>#6:00:00 PM#,DateDiff('h',#6:00:00 PM#,TimeValue([time_finish])),0)

1 个答案:

答案 0 :(得分:0)

我有一个旧函数来计算"正常" workhours:

Public Function ISO_WorkTimeDiff( _
  ByVal datDateTimeFrom As Date, _
  ByVal datDateTimeTo As Date, _
  Optional ByVal booNoHours As Boolean) _
  As Long

' Purpose: Calculate number of working minutes between date/times datDateTimeFrom and datDateTimeTo.
' Assumes: 5 or 6 working days per week. Weekend is (Saturday and) Sunday.
' Returns: "Working minutes". Divide by 60 to obtain working hours.
' Limitation: Does not count for public holidays.
'
' May be freely used and distributed.
' 2001-06-26. Gustav Brock, Cactus Data ApS, Copenhagen
'
' If booNoHours is True, time values are ignored.

  ' Specify begin and end time of daily working hours.
  Const cdatWorkTimeStart   As Date = #8:00:00 AM#
  Const cdatWorkTimeStop    As Date = #4:00:00 PM#
  Const cbytWorkdaysOfWeek  As Byte = 5

  Dim bytSunday             As Byte
  Dim intWeekdayDateFrom    As Integer
  Dim intWeekdayDateTo      As Integer
  Dim datTimeFrom           As Date
  Dim datTimeTo             As Date
  Dim lngDays               As Long
  Dim lngMinutes            As Long
  Dim lngWorkMinutesDaily   As Long

  ' No special error handling.
  On Error Resume Next

  If DateDiff("n", datDateTimeFrom, datDateTimeTo) <= 0 Then
    ' Nothing to do. Return zero.
  Else

    ' Calculate number of daily "working minutes".
    lngWorkMinutesDaily = DateDiff("n", cdatWorkTimeStart, cdatWorkTimeStop)

    ' Find ISO weekday for Sunday.
    bytSunday = Weekday(vbSunday, vbMonday)

    ' Find weekdays for the dates.
    intWeekdayDateFrom = Weekday(datDateTimeFrom, vbMonday)
    intWeekdayDateTo = Weekday(datDateTimeTo, vbMonday)

    ' Compensate weekdays' value for non-working days (weekends).
    intWeekdayDateFrom = intWeekdayDateFrom + (intWeekdayDateFrom = bytSunday)
    intWeekdayDateTo = intWeekdayDateTo + (intWeekdayDateTo = bytSunday)

    ' Calculate number of working days between the weeks of the two dates.
    lngDays = (cbytWorkdaysOfWeek * DateDiff("w", datDateTimeFrom, datDateTimeTo, vbMonday, vbFirstFourDays))
    ' Add number of working days between the two weekdays, ignoring number of weeks.
    lngDays = lngDays + intWeekdayDateTo - intWeekdayDateFrom - (cbytWorkdaysOfWeek * (intWeekdayDateTo < intWeekdayDateFrom))

    If Not booNoHours = True Then
      ' Extract begin and stop hour (time) for the working period.
      datTimeFrom = TimeSerial(Hour(datDateTimeFrom), Minute(datDateTimeFrom), Second(datDateTimeFrom))
      datTimeTo = TimeSerial(Hour(datDateTimeTo), Minute(datDateTimeTo), Second(datDateTimeTo))
      ' Adjust times before or after daily working hours to boundaries of working hours.
      If DateDiff("n", datTimeFrom, cdatWorkTimeStart) > 0 Then
        datTimeFrom = cdatWorkTimeStart
      ElseIf DateDiff("n", datTimeFrom, cdatWorkTimeStop) < 0 Then
        datTimeFrom = cdatWorkTimeStop
      End If
      If DateDiff("n", datTimeTo, cdatWorkTimeStart) > 0 Then
        datTimeTo = cdatWorkTimeStart
      ElseIf DateDiff("n", datTimeTo, cdatWorkTimeStop) < 0 Then
        datTimeTo = cdatWorkTimeStop
      End If

      ' Calculate number of working minutes between the two days, ignoring number of days.
      lngMinutes = DateDiff("n", datTimeFrom, datTimeTo)
    End If

    ' Calculate number of working minutes between the two days using the workday count.
    lngMinutes = lngMinutes + (lngDays * lngWorkMinutesDaily)

  End If

  ISO_WorkTimeDiff = lngMinutes

End Function

但是,开始时间必须(当然)在停止时间之前,所以对于你的情况,你需要加一天来穿越午夜:

  Const cdatWorkTimeStart   As Date = #6:00:00 PM#
  Const cdatWorkTimeStop    As Date = #6:00:00 AM# + 1

这似乎有效。要获得一小时计数,除以60:

WorkHours = ISO_WorkTimeDiff(DateTimeFrom, DateTimeTo) / 60

或者,对于向下舍入的整数计数:

WorkHours = ISO_WorkTimeDiff(DateTimeFrom, DateTimeTo) \ 60