Difference Between Two Times - Is it 30 minutes or more?

Ark68

Well-known Member
Joined
Mar 23, 2004
Messages
4,564
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I have two times, curr_st and prev_et.
I need to figure out how many minutes there are between the two times.
If there is 30 minutes or more between the two times then do one thing (set svc_off = curr_tt - 30 minutes), otherwise, do another thing (set svc_off - curr_st)

This is my code:
VBA Code:
tdiff = curr_st - prev_et 'time difference
If tdiff < 0.5 Then
     svc_off = curr_st
Else
     svc_off = curr_st - TimeSerial(0, 30, 0)
End If

When curr_st = 45117.75 (6:00pm), and prev_et = 45117.6145833 (2:45pm), there is clearly 30 minutes plus between the two times.
But my code triggers the "If tdiff < 0.5 then" statement, which is wrong.

I think the "If tdiff < 0.5 then" is wrong.

How do I correct this?
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
You need to keep in mind that 0.5 does not equal 0.5h (half an hour) but 0.5d (half a day) since 1 equals one whole day (24h)
Hence 30 minutes would be calculated as follows:

1h = 1/24d
0.5h = 1/48d = 0.02...

VBA Code:
tdiff = curr_st - prev_et 'time difference
If tdiff < 0.02 Then
     'less then 30 minutes
Else
     '30 minutes or more
End If
 
Upvote 1
Solution
Another option...

VBA Code:
Sub mindiff()
Dim tdiff As integer, prev_et As Date
Dim curr_st As Date, svc_off As Date

prev_et = TimeValue("6:45")
curr_st = TimeValue("7:30")

tdiff = DateDiff("n", prev_et, curr_st) 'n is minutes

If tdiff < 30 Then
     svc_off = curr_st
Else
     svc_off = curr_st - TimeSerial(0, 30, 0)
End If
End Sub
 
Upvote 1
Thank you both for your solutions. Both worked. Solution credit to you as well Mark.
 
Upvote 0

Forum statistics

Threads
1,215,013
Messages
6,122,690
Members
449,092
Latest member
snoom82

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top