+ Reply to Thread
Results 1 to 3 of 3

Calculate working hours between two dates/times

Hybrid View

  1. #1
    Registered User
    Join Date
    03-22-2016
    Location
    United States, NC
    MS-Off Ver
    2010
    Posts
    40

    Post Calculate working hours between two dates/times

    I am need the following formula in Microsoft SharePoint to calculate the number of hours between two dates and times, for working hours and exclude the weekends.

    =IF(AND((WEEKDAY([Date & Time Request Ends],2))<(WEEKDAY([Date & Time request Starts],2)),((WEEKDAY([Date & Time request Starts],2))-(WEEKDAY([Date & Time Request Ends],2)))>1),(((DATEDIF([Date & Time request Starts],[Date & Time Request Ends],"D")+1))-(FLOOR((DATEDIF([Date & Time request Starts],[Date & Time Request Ends],"D")+1)/7,1)*2)-2),(((DATEDIF([Date & Time request Starts],[Date & Time Request Ends],"D")+1))-(FLOOR((DATEDIF([Date & Time request Starts],[Date & Time Request Ends],"D")+1)/7,1)*2)))

    I tried adding this to the end of the formula but it didn't work.

    INT(([Date & Time Request Ends]-[Date & Time request Starts])*24)
    Last edited by Drscott21; 04-03-2018 at 01:02 PM. Reason: "Need Help with Sharepoint formula"

  2. #2
    Forum Expert tim201110's Avatar
    Join Date
    10-23-2011
    Location
    Russia
    MS-Off Ver
    2016, 2019
    Posts
    2,357

    Re: Calculate working hours between two dates/times

    =(NETWORKDAYS(B1,B2)-1)*("20:00"-"8:00")-C1-"12:00"+C2
    row1 -start, row2 -end

  3. #3
    Forum Expert dflak's Avatar
    Join Date
    11-24-2015
    Location
    North Carolina
    MS-Off Ver
    365
    Posts
    7,957

    Re: Calculate working hours between two dates/times

    The following are two user-defined function analogous to WorkDays and NetWorkDays.

    WorkHours (StartDate, NumerofHours, Shift_Start_Time, Shift_End_Time, [Holidays]) - computes end time of job given a start time and number of hours.

    NetWorkHours(Shift_Start_Time, Shift_End_Time, Start_Time, End_Time,[Holidays]) - computes number of hours worked given a start time and end time

    Function WorkHours(StartDate As Date, NumHours As Double, ShiftStart As Date, ShiftEnd As Date, _
        Optional Holidays As Range) As Date
    Application.Volatile
    Dim StartDay As Date            ' Start of the workday
    Dim EndDay As Date              ' End of the workday
    Dim EndHours As Date            ' Ending time of project
    Dim EndDate As Date             ' Ending date of project
    Dim DayHours As Double          ' Number of hours in the workday
    Dim BeginTime As Date           ' Beginning of task or shift
    
    ' Initialize variables
    EndDate = StartDate + NumHours / 24
    EndDay = Int(StartDate) + ShiftEnd
    BeginTime = StartDate - Int(StartDate)
    
    ' While the end date still exceeds the end of day for a workday
    While EndDate > EndDay
        ' increment EndDay
        EndDay = EndDay + 1
        ' Process only for workdays
        If IsWorkday(EndDay, Range("Holidays")) = True Then
            'decrement the hours
            NumHours = NumHours - 24 * (ShiftEnd - BeginTime)
            BeginTime = ShiftStart  ' Begin time becomes shift start after the first day
            ' Compute new enddate
            EndDate = Int(EndDay) + ShiftStart + NumHours / 24
        Else
            ' increment the end date for weekends and holidays without processing
            EndDate = EndDate + 1
        End If
    Wend
    
    WorkHours = EndDate
    End Function
    
    Function IsWorkday(MyDate As Date, Optional Holidays As Range) As Boolean
    Dim cl As Range             ' Pointer to holiday range
    Dim bRtn As Boolean         ' Return value (default = True unless proven otherwise)
    Dim WkDay As Long
    
    bRtn = True
    
    ' Check to see if the date is on the holiday list
    If Not Holidays Is Nothing Then
        For Each cl In Holidays
            If Int(MyDate) = cl.Value Then
                bRtn = False
                Exit For
         End If
        Next
    End If
    
    ' Check to see if the day is a weekend
    WkDay = Weekday(MyDate, vbMonday)
    
    If WkDay > 5 Then
        bRtn = False
    End If
    
    IsWorkday = bRtn
           
    End Function
    Function NetWorkHours(Shift_Start As Date, Shift_End As Date, Start_Time As Date, End_Time As Date, _
        Optional Holidays As Range) As Double
    Dim NumHrs As Double
    Dim ShiftHrs As Double
    Dim DayDiff As Long
    Dim DayLoop As Long
    Dim DayHoliday As Boolean
    
    ShiftHrs = Shift_End - Shift_Start
    NumHrs = 0
    
    ' Figure out the number of hours
    If Time_Fraction(Start_Time) <= Time_Fraction(End_Time) Then
        NumHrs = Time_Fraction(End_Time) - Time_Fraction(Start_Time)
    Else
        NumHrs = (Time_Fraction(Shift_End) - Time_Fraction(Start_Time)) + _
        (Time_Fraction(End_Time) - Time_Fraction(Shift_Start))
    End If
    
    ' Determine the number of extra days to add
    DayDiff = Int(End_Time - Start_Time)
    For DayLoop = 1 To DayDiff
        ' check to see if day is a weekend or holiday
        If Holidays Is Nothing Then
            DayHoliday = False
        Else
            DayHoliday = Is_Holiday(Int(Start_Time) + DayLoop)
        End If
        If Weekday(Int(Start_Time) + DayLoop, 2) < 6 And DayHoliday = False Then
            NumHrs = NumHrs + ShiftHrs
        End If
    Next DayLoop
    
    NetWorkHours = 24 * NumHrs
    
    End Function
    
    Function Is_Holiday(MyDate As Date) As Boolean
    Dim cl As Range                         ' Pointer to Holiday list
    
    ' Set to true if date is on the list of holidays
    For Each cl In Range("Holidays")
        If MyDate = cl.Value Then
            Is_Holiday = True
            Exit Function
        End If
    Next cl
    
    Is_Holiday = False
    End Function
    
    Function Time_Fraction(MyTime As Date) As Double
    Time_Fraction = TimeSerial(Hour(MyTime), Minute(MyTime), Second(MyTime))
    End Function
    One spreadsheet to rule them all. One spreadsheet to find them. One spreadsheet to bring them all and at corporate, bind them.

    A picture is worth a thousand words, but a sample spreadsheet is more likely to be worked on.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. VBA to select SharePoint Content Type when saving Excel file to SharePoint
    By Luffk73 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-24-2017, 05:22 PM
  2. Formula to access the server document properties linked to SharePoint
    By sraoof in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 07-13-2016, 03:45 AM
  3. Need Help Creating a Letter Grade excel formula to be used in SharePoint.
    By probodyrepair in forum Excel Formulas & Functions
    Replies: 17
    Last Post: 03-13-2015, 04:46 PM
  4. sharepoint formula time and date calculation
    By cmccabe in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 08-01-2014, 09:00 PM
  5. need help with sharepoint 2013 calculated column formula
    By xxxxmints in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 07-06-2014, 11:07 PM
  6. Opening SharePoint file with VBA - disable SharePoint "Contacting the server" message
    By kopite2002 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 10-22-2013, 01:56 AM
  7. Replies: 0
    Last Post: 10-08-2013, 10:54 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1