Link to home
Start Free TrialLog in
Avatar of Larry Brister
Larry BristerFlag for United States of America

asked on

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.

Page Load Event
RadDateTimePicker1.SelectedDate = RoundNext15(Now())

    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

Open in new window

Avatar of Ken Butters
Ken Butters
Flag of United States of America image

change:

(dateTime.Minute / 15) * 15

to:

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

wait... that won't work.... that rounds down...
Avatar of Larry Brister

ASKER

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

Open in new window

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

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
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.
@Julian.... I did use mod function. :)
"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...
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
nd = dt.AddSeconds(secs)
Console.WriteLine(nd)

Open in new window

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.