# round time DOWN to nearest 15 mins

Hello,

How can I round a time down to the nearest 15 minutes ?

this is what im kind thiking but it dosent work

ResTime = ResTime.Date.AddMinutes(-(DatePart(DateInterval.Minute, ResTime) Mod 15))

Thanks

Rob
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
Option Explicit

Private Sub Command1_Click()
Debug.Print fRoundTime15(#1:59:59 PM#)
End Sub

Public Function fRoundTime15(dtTime As Date) As Date
Dim intHour As Integer
Dim intMin As Integer
Dim i As Integer

intHour = Hour(dtTime)
intMin = Minute(dtTime)
For i = 0 To 60 Step 15
If i > intMin Then
intMin = i - 15
Exit For
End If
Next i
fRoundTime15 = TimeSerial(intHour, intMin, 0)
End Function
0
Commented:
Public Function RoundToNearest15Min(dtTime As Date) As Date
Dim intHour As Integer
Dim intMin As Integer
Dim i As Integer

intHour = Hour(dtTime)
intMin = Minute(dtTime)

memdiff = 61
For i = 0 To 60 Step 15
'Use Abs() function to get the nearest
If Abs(intMin - (i)) < memdiff Then
memdiff = Abs(intMin - (i)): RoundedMin = i
End If
Next i
RoundToNearest15Min = TimeSerial(intHour, RoundedMin, 0)
End Function

Private Sub Command1_Click()

Dim dtTime As Date

dtTime = #1:46:59 PM#
Debug.Print RoundToNearest15Min(dtTime)

dtTime = #1:58:59 PM#
Debug.Print RoundToNearest15Min(dtTime)

End Sub

0
Commented:
Private Function RoundTime(ByVal dtTime As Date) As Date
Dim lMins As Long
Dim lSecs As Long

lMins = CLng(Format(dtTime, "nn")) Mod 15
lSecs = CLng(Format(dtTime, "ss"))
End Function
0
Commented:
Just after my last submission, I realised you could also do:

Private Function RoundTime(ByVal dtTime As Date) As Date
Dim lMins As Long
Dim lSecs As Long

lMins = DatePart("n", dtTime) Mod 15
lSecs = DatePart("s", dtTime)
End Function
0

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Author Commented:
cheers mate !!
0
Commented:
Nice solution.
0
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.