Announcement

Collapse
No announcement yet.

Time Difference Function

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Time Difference Function



    Hi,

    I haven't had much experience with writing functions before. I have a situation where I want to know the number of seconds between two times, I am checking a roster of events against a list of when things actually happened (so 9:00 - 9:15 should equal 900, and 9:15 - 9:00 should equal 900). The following is the function I came up with (very basic). Can anyone tell me if there is a different way of doing this?


    Function TimeDiff(StartTime As Date, StopTime As Date)
    Dim TempTime As Date

    StartTime = FormatDateTime(StartTime, vbShortTime)
    StopTime = FormatDateTime(StopTime, vbShortTime)

    If StartTime > StopTime Then
    TempTime = StartTime
    StartTime = StopTime
    StopTime = TempTime
    End If

    TimeDiff = DateDiff("s", StartTime, StopTime)
    End Function

  • #2
    Re: Time Difference Function

    No need for a UDF.

    Use the following formula


    =abs(A1-B1)*86400

    Where A1 is time 1 and B1 is time 2. The abs makes it possible so that neither time has to be before or after the other.

    If you need to use the UDF that you've created then:
    Code:
    Function TimeDiff(StartTime As Date, StopTime As Date) 
        TimeDiff = abs(StopTime-StartTime) * 86400 
    End Function
    Last edited by iwrk4dedpr; January 29th, 2006, 12:35.
    Regards,
    Barry

    My Favorite New Thing:
    Dynamic Named Ranges



    The alternative for
    "Press Any Key To Continue."

    and we all have one we'd like to use it on

    1. Cross Posting Etiquette
    2. Are You Here To Learn: What Have You Tried?
    3. Your Best Resource to begin learning VBA: RECORD A MACRO . . . Then Study It!

    Comment


    • #3


      Re: Time Difference Function

      Thanks,

      Always something new to learn.

      Andy

      Comment

      Working...
      X