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

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

0
lrbrister
Asked:
lrbrister
  • 4
  • 2
  • 2
  • +1
1 Solution
 
Ken ButtersCommented:
change:

(dateTime.Minute / 15) * 15

to:

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

wait... that won't work.... that rounds down...
0
 
lrbristerAuthor 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

Open in new window

0
 
Ken ButtersCommented:
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

0
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
Mike TomlinsonMiddle 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

Open in new window

0
 
Ken ButtersCommented:
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
 
Julian HansenCommented:
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
 
Ken ButtersCommented:
@Julian.... I did use mod function. :)
0
 
Mike TomlinsonMiddle 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
 
Julian HansenCommented:
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

0
 
lrbristerAuthor 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

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

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