?
Solved

round time DOWN to nearest 15 mins

Posted on 2003-03-08
6
Medium Priority
?
915 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
Comment
Question by:Rob_2002
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 38

Expert Comment

by:PaulHews
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

by:DocM
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

by:phildaley
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Accepted Solution

by:
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

by:Rob_2002
ID: 8097107
cheers mate !!
0
 
LVL 38

Expert Comment

by:PaulHews
ID: 8097582
Nice solution.
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

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

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

Join & Ask a Question