Ozgrid Excel Help & Best Practices Forums

Excel Training / Excel Dashboards Reports

1. ## 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. ## 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 12:35.

3. ## Re: Time Difference Function

Thanks,

Always something new to learn.

Andy

Excel Video Tutorials / Excel Dashboards Reports

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

#### 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