Hi,
I have this kind of output so far for rounding to 10 mins.
As you see from the first section, i have rounded down where minutes <=4 ( less than half way )
I have roundedto10 minutes if between 5 and 14 minutes.
The code to do all this is at the bottom of this lot.
Just add a large richtextbox to a FORM to see the output.
I have created a function called
RoundMyDateTime in this code example.
See the blue and white line that looks
like this
below, for an example of how to use it.
Regards,
S_DS
18/07/2007 00:0:00 18/07/2007 00:00:00
18/07/2007 00:1:00 18/07/2007 00:00:00
18/07/2007 00:2:00 18/07/2007 00:00:00
18/07/2007 00:3:00 18/07/2007 00:00:00
18/07/2007 00:4:00 18/07/2007 00:00:00
18/07/2007 00:5:00 18/07/2007 00:10:00
18/07/2007 00:6:00 18/07/2007 00:10:00
18/07/2007 00:7:00 18/07/2007 00:10:00
18/07/2007 00:8:00 18/07/2007 00:10:00
18/07/2007 00:9:00 18/07/2007 00:10:00
18/07/2007 00:10:00 18/07/2007 00:10:00
18/07/2007 00:11:00 18/07/2007 00:10:00
18/07/2007 00:12:00 18/07/2007 00:10:00
18/07/2007 00:13:00 18/07/2007 00:10:00
18/07/2007 00:14:00 18/07/2007 00:10:00
18/07/2007 00:15:00 18/07/2007 00:20:00
18/07/2007 00:16:00 18/07/2007 00:20:00
18/07/2007 00:17:00 18/07/2007 00:20:00
18/07/2007 00:18:00 18/07/2007 00:20:00
18/07/2007 00:19:00 18/07/2007 00:20:00
18/07/2007 00:20:00 18/07/2007 00:20:00
18/07/2007 00:21:00 18/07/2007 00:20:00
18/07/2007 00:22:00 18/07/2007 00:20:00
18/07/2007 00:23:00 18/07/2007 00:20:00
18/07/2007 00:24:00 18/07/2007 00:20:00
18/07/2007 00:25:00 18/07/2007 00:30:00
18/07/2007 00:26:00 18/07/2007 00:30:00
18/07/2007 00:27:00 18/07/2007 00:30:00
18/07/2007 00:28:00 18/07/2007 00:30:00
18/07/2007 00:29:00 18/07/2007 00:30:00
18/07/2007 00:30:00 18/07/2007 00:30:00
18/07/2007 00:31:00 18/07/2007 00:30:00
18/07/2007 00:32:00 18/07/2007 00:30:00
18/07/2007 00:33:00 18/07/2007 00:30:00
18/07/2007 00:34:00 18/07/2007 00:30:00
18/07/2007 00:35:00 18/07/2007 00:40:00
18/07/2007 00:36:00 18/07/2007 00:40:00
18/07/2007 00:37:00 18/07/2007 00:40:00
18/07/2007 00:38:00 18/07/2007 00:40:00
18/07/2007 00:39:00 18/07/2007 00:40:00
18/07/2007 00:40:00 18/07/2007 00:40:00
18/07/2007 00:41:00 18/07/2007 00:40:00
18/07/2007 00:42:00 18/07/2007 00:40:00
18/07/2007 00:43:00 18/07/2007 00:40:00
18/07/2007 00:44:00 18/07/2007 00:40:00
18/07/2007 00:45:00 18/07/2007 00:50:00
18/07/2007 00:46:00 18/07/2007 00:50:00
18/07/2007 00:47:00 18/07/2007 00:50:00
18/07/2007 00:48:00 18/07/2007 00:50:00
18/07/2007 00:49:00 18/07/2007 00:50:00
18/07/2007 00:50:00 18/07/2007 00:50:00
18/07/2007 00:51:00 18/07/2007 00:50:00
18/07/2007 00:52:00 18/07/2007 00:50:00
18/07/2007 00:53:00 18/07/2007 00:50:00
18/07/2007 00:54:00 18/07/2007 00:50:00
18/07/2007 00:55:00 18/07/2007 01:00:00
18/07/2007 00:56:00 18/07/2007 01:00:00
18/07/2007 00:57:00 18/07/2007 01:00:00
18/07/2007 00:58:00 18/07/2007 01:00:00
18/07/2007 00:59:00 18/07/2007 01:00:00
Public
Class Form1
Dim myHour As Integer
Dim myMinutes As Integer
Dim mySeconds As Integer
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim fullDateTimeString As String
Dim roundedDate As Date
Dim dateToRound As Date
Me.Show()
For min As Integer = 0 To 59
fullDateTimeString = "18/07/2007 00:" & min.ToString & ":00"
dateToRound =
CDate(fullDateTimeString)
roundedDate = RoundMyDateTime(dateToRound, 10)
RichTextBox1.AppendText(fullDateTimeString & vbTab & vbTab)
RichTextBox1.AppendText(
" " & roundedDate.ToShortDateString)
RichTextBox1.AppendText(
" " & roundedDate.ToLongTimeString & vbNewLine)
Next
End Sub
Public Function RoundMyDateTime(ByVal dt1 As Date, ByVal roundInterval As Integer) As Date
myMinutes = dt1.Minute
mySeconds = dt1.Second
'Round the minutes based on the seconds.
Select Case mySeconds
Case Is < 30
mySeconds = 0
'If greater than 30 add 1 to the mins.
Case Is > 30
mySeconds = 0
myMinutes += 1
End Select
Dim myInterval As Integer = roundInterval
Dim remainder As Double = 0
remainder =
CDbl(myMinutes) / CDbl(myInterval) - Int((CDbl(myMinutes) / CDbl(myInterval)))
Select Case remainder
'Round down if less than half way.
Case Is < 0.5
myMinutes = myInterval *
CInt(Int((CDbl(myMinutes) / CDbl(myInterval))))
'Round up if half way or more.
Case Is >= 0.5
myMinutes = myInterval *
CInt(Int((CDbl(myMinutes) / CDbl(myInterval)) + 1))
End Select
'Round the hour if mins>=58
If myMinutes >= 58 Then
myMinutes = 0
myHour = dt1.Hour + 1
If dt1.Hour = 23 Then myHour = 0
End If
Dim dateString As String = ""
dateString &= dt1.Day.ToString
dateString &=
"/" & dt1.Month.ToString
dateString &=
"/" & dt1.Year.ToString
dateString &=
" " & myHour.ToString
dateString &=
":" & myMinutes.ToString
dateString &=
":" & mySeconds.ToString
Return CDate(dateString)
End Function
End
Class