Loading
Ozgrid Excel Help & Best Practices Forums

Excel Video Tutorials / Excel Dashboards Reports



Results 1 to 3 of 3

Thread: Time Difference Function

  1. #1
    Join Date
    16th November 2005
    Location
    New Zealand
    Posts
    129

    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?

    VB:
    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 
    
    

    Excel Video Tutorials / Excel Dashboards Reports


  2. #2
    Join Date
    22nd January 2004
    Location
    Colorado Springs
    Posts
    4,009

    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:
    VB:
    Function TimeDiff(StartTime As Date, StopTime As Date) 
        TimeDiff = abs(StopTime-StartTime) * 86400 
    End Function 
    
    
    Last edited by iwrk4dedpr; January 29th, 2006 at 11: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!

  3. #3
    Join Date
    16th November 2005
    Location
    New Zealand
    Posts
    129

    Re: Time Difference Function

    Thanks,

    Always something new to learn.

    Andy

    Excel Video Tutorials / Excel Dashboards Reports


Thread Information

Users Browsing this Thread

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

Possible Answers

  1. Calculate Time Difference Between Decimal Time
    By jshayler in forum EXCEL HELP
    Replies: 4
    Last Post: August 21st, 2010, 03:58
  2. Calculate Time Difference In Decimal Time
    By DexKnows in forum EXCEL HELP
    Replies: 5
    Last Post: August 16th, 2008, 09:16
  3. Time Difference
    By rupeshnk in forum EXCEL HELP
    Replies: 7
    Last Post: February 14th, 2007, 21:52
  4. format time difference from time in ranges
    By DISCARNET in forum EXCEL HELP
    Replies: 3
    Last Post: July 8th, 2006, 21:33
  5. Time difference
    By malay_sarkar in forum EXCEL HELP
    Replies: 4
    Last Post: February 18th, 2005, 15:27

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
  •  
porno