• Status: Solved
• Priority: Medium
• Security: Public
• Views: 1043

# vb code next quarter hour

I'm trying to set a RadDateTimePicker to be set on page load to the next quarter hour

In other words, if page loads on 10/1/2012 as 12:23pm the set date and hour is

10/1/2012 12:30pm

I already have the intervals set etc on the control

I'm trying to get this to work...but having brain gas day for some reason.

``````    Private Function RoundNext15(ByVal dateTime As DateTime) As DateTime
Return New DateTime(dateTime.Year, dateTime.Month, dateTime.Day, dateTime.Hour, (dateTime.Minute / 15) * 15, 0)
End Function
``````
0
lrbrister
• 4
• 2
• 2
• +1
1 Solution

Commented:
change:

(dateTime.Minute / 15) * 15

to:

CInt((dateTime.Minute / 15)) * 15

wait... that won't work.... that rounds down...
0

Author Commented:
buttersk

I have this which works...but clunky.  And got the feeling there are holes in it as well
``````    Private Function RoundNext15(ByVal dateTime As DateTime) As DateTime
Dim _m As Integer
If CInt(dateTime.Minute) >= 0 And CInt(dateTime.Minute) <= 15 Then
_m = 15
ElseIf CInt(dateTime.Minute) > 15 And CInt(dateTime.Minute) <= 30 Then
_m = 30
ElseIf CInt(dateTime.Minute) > 30 And CInt(dateTime.Minute) <= 59 Then
_m = 45
Else
_m = 0
End If
Return New DateTime(dateTime.Year, dateTime.Month, dateTime.Day, dateTime.Hour, _m, 0)
End Function
``````
0

Commented:
here what I came up with .... i'm thinking it is easier to round down... then add 15 minutes.... except... where you were already on an exact 15 min boundary... then you wouldn't want to round down at all.

so first an if statement to weed out where no rounding is required...

``````    Private Function RoundNext15(ByVal dateTime As DateTime) As DateTime

Dim duration As System.TimeSpan

duration = New System.TimeSpan(0, 0, 15, 0)

if  dateTime.minute mod 15 = 0 then
return dateTime
end if

Return New DateTime(dateTime.Year, dateTime.Month, dateTime.Day, dateTime.Hour, Cint(dateTime.Minute / 15) * 15, 0)+duration
End Function
``````
0

Middle School Assistant TeacherCommented:
Use:
``````    Private Function RoundNext15(ByVal dateTime As DateTime) As DateTime
Dim mins As Integer = (dateTime.Minute \ 15) + 1 ' INTEGER division (backslash instead of forward slash!)
Return New DateTime(dateTime.Year, dateTime.Month, dateTime.Day, dateTime.Hour, 0, 0).AddMinutes(mins * 15)
End Function
``````
0

Commented:
Note... one of the holes your method does not consider.... what if rounding up to the next 15 minute pushes you into the next hour or next day .... etc...

so adding 15 minutes to the rounded down value should account for all of that.

.... @idle_mind... note you don't want to round up 8:15 to 8:30 for example.
0

Commented:
Why not use the mod function

15 - mins mod 15 gives you an amount you are short of the next 15 min interval so

if 15 - (mins mod 15) <> 0
then mins = mins + 15 - (mins mod 15)

No time to write code in VB but do this all the time in other dev envs.
0

Commented:
@Julian.... I did use mod function. :)
0

Middle School Assistant TeacherCommented:
"what if rounding up to the next 15 minute pushes you into the next hour or next day"

It will work because I'm setting the initial DateTime with a 0 (zero) in the Minute field and then using AddMinutes() to push it forward to the correct time.  If we add 60 minutes it will properly roll to the next hour.

"you don't want to round up 8:15 to 8:30 for example"

The original description asks for the "next quarter hour".  If it is already 15 or 30, then in my opinion the "next" one should be 30 and 45 respectively.  ...since we are already PAST that quarter hour (if you look at the seconds value; time is still marching on!).  It really depends on what you're going to do with this value I suppose...
0

Commented:
``````Dim dt As DateTime = Date.Now
Dim nd As DateTime
Dim secs As Integer = dt.Minute * 60 + dt.Second
' Ensures a clean second boundary
If secs Mod 900 <> 0 Then
secs = 900 - (secs Mod 900)
End If
' Don't need to worry about carry over on hours DateTime sorts that for you
Console.WriteLine(nd)
``````
0

Author Commented:
Guys,
Thanks for all your input and hard work

In my estimation, this answer best fits the question.

idel had a point on the "15" minute question.
Time IS marching on and it's a valid point to grab the next 15.
0

## Featured Post

• 4
• 2
• 2
• +1
Tackle projects and never again get stuck behind a technical roadblock.