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

Ark68

Well-known Member
Joined
Mar 23, 2004
Messages
4,570
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

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
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,223,903
Messages
6,175,287
Members
452,631
Latest member
a_potato

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