Update time to the nearest 15 minutes

Patty05

Active member
Joined
Aug 28, 2006
Messages
29
Programming Experience
Beginner
I am trying to create somewhat of a time clock program. When the user punches in, I need the current time to round to the nearest 15 minute interval. For example, if a user punches in at 6:50 am, it needs to write to the database 7:00 am. If a user punches in at 8:05, it needs to read 8:15.

I have tried using

todayDate = Convert.ToInt32(Today())

If Not (todayDate Mod 15 = 0) Then
do something with it code here
End If

But I get a conversion error where I cannot convert datetime to Integer.

My question is, how do I round the current time up or down to the nearest 15 minute interval?

Thanks so much.
 
This simple function seems to work...

VB.NET:
Function RoundToQuarterHour(ByVal inDateTime As DateTime) As DateTime

Dim minutes As Integer
Dim addMinutes As Double

minutes = inDateTime.Minute

Do While (minutes + addMinutes) Mod 15 <> 0
  addMinutes += 1
Loop

inDateTime = inDateTime.AddMinutes(addMinutes)

RoundToQuarterHour = inDateTime

End Function
 
The code can be shortened some:
VB.NET:
Function RoundUpToQuarterHour(ByVal theDate As Date) As Date
  Do Until theDate.Minute Mod 15 = 0
    theDate = theDate.AddMinutes(1)
  Loop
  Return theDate
End Function
 
Right. My code comes from the old advice that if you're accessing a property a lot, use a variable. Your code can make many calls to the object. Mine makes 2. Maybe in the fast-lane world of VB.NET 2005 that advice no longer applies?
 
There could be situations where a property have to be calculated (or timely retrieved) internally in the class and would take longer time to do than just grabbing it into a temporary variable to be operated, but in this case my method is faster and it's more readable, it's less work for the programmer and it places less workload on GC/memory allocations. You can time them with the StopWatch class if you want.

Even if there was internal work for a class object to produce the property value, it would depend on the usage if it was worth it for the developer to do the extra stuff as in your example, if the method was only called once every second I doubt there would be any performance gain after all. I would say always doing this for properties is a very bad programming habit.

Here is one modification of my method that uses a ByRef Sub method. This is even faster than my function example, because there is two less memory allocations (the byval input parameter and the return value), but it is more often the convension of such routine to produce a new value (function) than to modify an existing (byref sub).
VB.NET:
Sub RoundUpToQuarterHour_2(ByRef theDate As Date)
  Do Until theDate.Minute Mod 15 = 0
    theDate = theDate.AddMinutes(1)
  Loop
End Sub
When I compared your method to my first with 10.000 calls in succession (time 12:01 calls), my first was 30% faster and my second was 32% faster, but in total not even a millisecond difference.
 
Well it's as I thought, I tend to hold on to old advice longer than necessary. When VB first had objects (4.0?) they would tell you not to make any more than you need. I guess their first pass at classes and objects was probably slow and took more memory than necessary. When I finally moved to Java I had to get over that, since then everything was an object. I adjusted but old habits die hard.

Thanks for the advice. I think you're 100% correct in your assessment of the situation.
 
Back
Top