Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
Solved

# round time DOWN to nearest 15 mins

Posted on 2003-03-08
Medium Priority
920 Views
Last Modified: 2008-02-01
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
0
Question by:Rob_2002
6 Comments

LVL 38

Expert Comment

ID: 8094875
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

LVL 3

Expert Comment

ID: 8095440
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

LVL 3

Expert Comment

ID: 8095977
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"))
dtTime = DateAdd("n", -lMins, dtTime)
RoundTime = DateAdd("s", -lSecs, dtTime)
End Function
0

LVL 3

Accepted Solution

phildaley earned 400 total points
ID: 8095998
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)
dtTime = DateAdd("n", -lMins, dtTime)
RoundTime = DateAdd("s", -lSecs, dtTime)
End Function
0

Author Comment

ID: 8097107
cheers mate !!
0

LVL 38

Expert Comment

ID: 8097582
Nice solution.
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
###### Suggested Courses
Course of the Month15 days, 20 hours left to enroll

#### 581 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.