Results 1 to 5 of 5

Thread: Need function that will return amount of time between two timestamps.

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2006
    Posts
    1

    Need function that will return amount of time between two timestamps.

    I looking for a function that I can use to enter two timestamps and return the amount of time between them (in minutes).

    Parameters are:
    Excel Cells will be formatted as follows (dd/mm/ccyy hh/mm/ss AM/PM)
    Do not include weekends in the calculation.
    Do not include the time between 5pm and 8am in the calculations.

    For example the values in the two cells may be:
    6/16/2006 1:46:14 PM
    6/19/2006 2:01:40 PM

    What it should return is 552 minutes and 6 seconds.(or 552.1)

    Does anyone know of a macro that does this or the code needed to build one?

    Thanks,
    DD.

  2. #2
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Need function that will return amount of time between two timestamps.

    u will need to write your own..

    DateDiff() can calc the diff in minutes.. but to remove the times between 5-8 will take some thinking and work.
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  3. #3
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Need function that will return amount of time between two timestamps.

    just messing around:

    this did NOT take into account if there is a weekend.. but it seemed to work ok otherwise

    VB Code:
    1. Dim t1 As Date
    2.     Dim t2 As Date
    3.     Dim t1F As Date
    4.     Dim t2F As Date
    5.     Dim d1_mins As Integer
    6.     Dim d2_mins As Integer
    7.     Dim minsbtween As Integer
    8.    
    9.     t1 = #6/16/2006 1:46:14 PM#
    10.     t2 = #6/19/2006 2:01:40 PM#
    11.    
    12.     t1F = CDate(Format(t1, "mm/dd/yyyy") & " 5:00:00 PM")
    13.     t2F = CDate(Format(t2, "mm/dd/yyyy") & " 8:00:00 AM")
    14.     d1_mins = DateDiff("n", t1, t1F)
    15.     d2_mins = DateDiff("n", t2F, t2)
    16.     minsbtween = (DateDiff("d", t1, t2) - 1) * 540
    17.     MsgBox d1_mins + d2_mins + minsbtween
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  4. #4
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Need function that will return amount of time between two timestamps.

    DDouglas
    Welcome to the forums

    In your example I think you are slightly off in you calculation
    13:46:14 to 17:00:00 is 3:13:46
    08:00:00 to 14:01:40 is 6:01:40

    giving a total of 9:15:26 or 33326 seconds or 555 mins and 26 secs or 555.43 mins.

    Here's a function that will give you that answer.
    VB Code:
    1. Option Explicit
    2.  
    3. Function DDouglasDateDiff(ByVal dDate1 As Date, ByVal dDate2 As Date) As Double
    4. Dim dbTemp As Double
    5. Dim lDay As Long
    6. Dim lCounter As Long
    7.  
    8.     'Move dDate1 forward to 8 AM on the same day,
    9.     'if its time portion is before 8
    10.     If DatePart("h", dDate1) < 8 Then
    11.         dDate1 = CDate(Left(CStr(dDate1), InStr(1, CStr(dDate1), " ") - 1) & " 08:00:00")
    12.     End If
    13.    
    14.     'Move dDate1 forward to 8 AM on the next day,
    15.     'if its time portion is after 5
    16.     If DatePart("h", dDate1) >= 17 Then
    17.         dDate1 = CDate(Left(CStr(dDate1 + 1), InStr(1, CStr(dDate1 + 1), " ") - 1) & " 08:00:00")
    18.     End If
    19.    
    20.     'Move dDate1 forward if it is a weekend
    21.     Do While Weekday(dDate1, vbMonday) > 5
    22.         dDate1 = CDate(Left(CStr(dDate1 + 1), InStr(1, CStr(dDate1 + 1), " ") - 1) & " 08:00:00")
    23.     Loop
    24.    
    25.     'Move dDate2 back to 5 PM on the same day,
    26.     'if its time portion is after 5
    27.     If DatePart("h", dDate2) >= 17 Then
    28.         dDate1 = CDate(Left(CStr(dDate2), InStr(1, CStr(dDate2), " ") - 1) & " 17:00:00")
    29.     End If
    30.    
    31.     'Move dDate2 back to 5 PM on the previous day,
    32.     'if its time portion is before 8
    33.     If DatePart("h", dDate2) < 8 Then
    34.         dDate1 = CDate(Left(CStr(dDate2 - 1), InStr(1, CStr(dDate2 - 1), " ") - 1) & " 17:00:00")
    35.     End If
    36.    
    37.     'Move dDate2 backward if it is a weekend
    38.     Do While Weekday(dDate2, vbMonday) > 5
    39.         dDate1 = CDate(Left(CStr(dDate2 - 1), InStr(1, CStr(dDate2 - 1), " ") - 1) & " 08:00:00")
    40.     Loop
    41.    
    42.     'Get the number of seconds between the dates
    43.     dbTemp = DateDiff("s", dDate1, dDate2)
    44.    
    45.     'Get the number of days between the dates
    46.     lDay = DateDiff("d", dDate1, dDate2)
    47.    
    48.     If lDay <> 0 Then
    49.         For lCounter = 1 To lDay
    50.             If Weekday(dDate1 + lCounter, vbMonday) > 5 Then
    51.                 'For weekends remove 24 hours
    52.                 dbTemp = dbTemp - 86400 '(24 hrs * 60 mins * 60 secs)
    53.             Else
    54.                 'For all other days, remove 15 hours (5PM - 8AM)
    55.                 dbTemp = dbTemp - 54000 '(15 hrs * 60 mins * 60 secs)
    56.             End If
    57.         Next lCounter
    58.     End If
    59.    
    60.     'Convert to minutes
    61.     DDouglasDateDiff = dbTemp / 60
    62.    
    63.    
    64. End Function
    Last edited by DKenny; Jun 13th, 2006 at 12:45 PM.
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  5. #5
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Need function that will return amount of time between two timestamps.

    Here's a tidier version, using the Format function, rather than manually parsing the "date" portion
    VB Code:
    1. Function DDouglasDateDiff(ByVal dDate1 As Date, ByVal dDate2 As Date) As Double
    2. Dim dbTemp As Double
    3. Dim lDay As Long
    4. Dim lCounter As Long
    5.  
    6.     'Move dDate1 forward to 8 AM on the same day,
    7.     'if its time portion is before 8
    8.     If DatePart("h", dDate1) < 8 Then
    9.         dDate1 = CDate(Format(dDate1, "yyyy/mm/dd") & " 08:00:00")
    10.     End If
    11.    
    12.     'Move dDate1 forward to 8 AM on the next day,
    13.     'if its time portion is after 5
    14.     If DatePart("h", dDate1) >= 17 Then
    15.         dDate1 = CDate(Format(dDate1 + 1, "yyyy/mm/dd") & " 08:00:00")
    16.     End If
    17.    
    18.     'Move dDate1 forward if it is a weekend
    19.     Do While Weekday(dDate1, vbMonday) > 5
    20.         dDate1 = CDate(Format(dDate1 + 1, "yyyy/mm/dd") & " 08:00:00")
    21.     Loop
    22.    
    23.     'Move dDate2 back to 5 PM on the same day,
    24.     'if its time portion is after 5
    25.     If DatePart("h", dDate2) >= 17 Then
    26.         dDate1 = CDate(Format(dDate2, "yyyy/mm/dd") & " 17:00:00")
    27.     End If
    28.    
    29.     'Move dDate2 back to 5 PM on the previous day,
    30.     'if its time portion is before 8
    31.     If DatePart("h", dDate2) < 8 Then
    32.         dDate1 = CDate(Format(dDate2 - 1, "yyyy/mm/dd") & " 17:00:00")
    33.     End If
    34.    
    35.     'Move dDate2 backward if it is a weekend
    36.     Do While Weekday(dDate2, vbMonday) > 5
    37.         dDate1 = CDate(Format(dDate2 - 1, "yyyy/mm/dd") & " 08:00:00")
    38.     Loop
    39.    
    40.     'Get the number of seconds between the dates
    41.     dbTemp = DateDiff("s", dDate1, dDate2)
    42.    
    43.     'Get the number of days between the dates
    44.     lDay = DateDiff("d", dDate1, dDate2)
    45.    
    46.     If lDay <> 0 Then
    47.         For lCounter = 1 To lDay
    48.             If Weekday(dDate1 + lCounter, vbMonday) > 5 Then
    49.                 'For weekends remove 24 hours
    50.                 dbTemp = dbTemp - 86400 '(24 hrs * 60 mins * 60 secs)
    51.             Else
    52.                 'For all other days, remove 15 hours (5PM - 8AM)
    53.                 dbTemp = dbTemp - 54000 '(15 hrs * 60 mins * 60 secs)
    54.             End If
    55.         Next lCounter
    56.     End If
    57.    
    58.     'Convert to minutes
    59.     DDouglasDateDiff = dbTemp / 60
    60.    
    61.    
    62. End Function
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

Posting Permissions

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



Click Here to Expand Forum to Full Width