VB.NET || How To Generate Hourly Time Range With Minute Interval Using VB.NET
The following is a module with functions which demonstrates how to generate an hourly time range with minute interval between a start and end time using VB.NET.
The function demonstrated in this page generates a time value list containing the time from a starting hour to an ending hour, separated by a minute step. The starting and ending hour is an integer from 0 through 23 representing the 24 hour period of the day, and the minute step can be any minute interval, including decimal values.
The starting and ending hours can also be flipped, indicating that the results should be returned in descending order.
An optional parameter also exists which allows to tune the results. When the range is in ascending order, the parameter indicates that the results should exclude/include the entire ending hour. When the range is in descending order, the parameter indicates that the results should exclude/include the entire starting hour.
1. Ascending Time Range – Minute Interval – Default
The example below demonstrates the use of ‘Utils.DateRange.GetTimeRange‘ to get the time range with a minute interval in ascending order.
In this example, the default options are used, which excludes the full ending hour from the results.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
' Ascending Time Range - Minute Interval - Default ' Get time range from 12AM - 11PM, with a 15 and half minute interval Dim result = Utils.DateRange.GetTimeRange(0, 23, 15.5) For Each value In result Debug.Print($"Time: {value.Time}, String: {value.ToString}") Next ' expected output: ' Time: 1/1/0001 12:00:00 AM, String: 12:00:00 AM ' Time: 1/1/0001 12:15:30 AM, String: 12:15:30 AM ' Time: 1/1/0001 12:31:00 AM, String: 12:31:00 AM ' Time: 1/1/0001 12:46:30 AM, String: 12:46:30 AM ' Time: 1/1/0001 1:02:00 AM, String: 1:02:00 AM ' Time: 1/1/0001 1:17:30 AM, String: 1:17:30 AM ' Time: 1/1/0001 1:33:00 AM, String: 1:33:00 AM ' Time: 1/1/0001 1:48:30 AM, String: 1:48:30 AM ' .... ' .... ' .... ' .... ' Time: 1/1/0001 9:11:00 PM, String: 9:11:00 PM ' Time: 1/1/0001 9:26:30 PM, String: 9:26:30 PM ' Time: 1/1/0001 9:42:00 PM, String: 9:42:00 PM ' Time: 1/1/0001 9:57:30 PM, String: 9:57:30 PM ' Time: 1/1/0001 10:13:00 PM, String: 10:13:00 PM ' Time: 1/1/0001 10:28:30 PM, String: 10:28:30 PM ' Time: 1/1/0001 10:44:00 PM, String: 10:44:00 PM ' Time: 1/1/0001 10:59:30 PM, String: 10:59:30 PM |
2. Descending Time Range – Hourly Interval – Default
The example below demonstrates the use of ‘Utils.DateRange.GetTimeRange‘ to get the time range with a hourly interval in descending order.
In this example, the default options are used, which excludes the full starting hour from the results.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
' Descending Time Range - Hourly Interval - Default ' Get an hour range from 10PM - 10AM, with a 2 hour interval Dim result = Utils.DateRange.GetTimeRange(22, 10, 120) For Each value In result Debug.Print($"Time: {value.Time}, String: {value.ToString(True)}") Next ' expected output: ' Time: 1/1/0001 10:00:00 PM, String: 10 PM ' Time: 1/1/0001 8:00:00 PM, String: 8 PM ' Time: 1/1/0001 6:00:00 PM, String: 6 PM ' Time: 1/1/0001 4:00:00 PM, String: 4 PM ' Time: 1/1/0001 2:00:00 PM, String: 2 PM ' Time: 1/1/0001 12:00:00 PM, String: 12 PM ' Time: 1/1/0001 10:00:00 AM, String: 10 AM |
3. Ascending Time Range – Minute Interval – Include Full Hour
The example below demonstrates the use of ‘Utils.DateRange.GetTimeRange‘ to get the time range with a minute interval in ascending order.
In this example, the optional parameter is used, which includes the full ending hour from the results.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
' Ascending Time Range - Minute Interval - Include Full Hour ' Get time range from 12AM - 11PM, with a 15 and half minute interval Dim result = Utils.DateRange.GetTimeRange(0, 23, 15.5, True) For Each value In result Debug.Print($"Time: {value.Time}, String: {value.ToString}") Next ' expected output: ' Time: 1/1/0001 12:00:00 AM, String: 12:00:00 AM ' Time: 1/1/0001 12:15:30 AM, String: 12:15:30 AM ' Time: 1/1/0001 12:31:00 AM, String: 12:31:00 AM ' Time: 1/1/0001 12:46:30 AM, String: 12:46:30 AM ' Time: 1/1/0001 1:02:00 AM, String: 1:02:00 AM ' Time: 1/1/0001 1:17:30 AM, String: 1:17:30 AM ' Time: 1/1/0001 1:33:00 AM, String: 1:33:00 AM ' Time: 1/1/0001 1:48:30 AM, String: 1:48:30 AM ' .... ' .... ' .... ' .... ' Time: 1/1/0001 10:13:00 PM, String: 10:13:00 PM ' Time: 1/1/0001 10:28:30 PM, String: 10:28:30 PM ' Time: 1/1/0001 10:44:00 PM, String: 10:44:00 PM ' Time: 1/1/0001 10:59:30 PM, String: 10:59:30 PM ' Time: 1/1/0001 11:15:00 PM, String: 11:15:00 PM ' Time: 1/1/0001 11:30:30 PM, String: 11:30:30 PM ' Time: 1/1/0001 11:46:00 PM, String: 11:46:00 PM |
4. Descending Time Range – Hourly Interval – Include Full Hour
The example below demonstrates the use of ‘Utils.DateRange.GetTimeRange‘ to get the time range with a hourly interval in descending order.
In this example, the optional parameter is used, which includes the full starting hour from the results.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
' Descending Time Range - Hourly Interval - Include Full Hour ' Get an hour range from 10PM - 10AM, with a 2 hour interval Dim result = Utils.DateRange.GetTimeRange(22, 10, 120, True) For Each value In result Debug.Print($"Time: {value.Time}, String: {value.ToString(True)}") Next ' expected output: ' Time: 1/1/0001 10:59:00 PM, String: 10:59 PM ' Time: 1/1/0001 8:59:00 PM, String: 8:59 PM ' Time: 1/1/0001 6:59:00 PM, String: 6:59 PM ' Time: 1/1/0001 4:59:00 PM, String: 4:59 PM ' Time: 1/1/0001 2:59:00 PM, String: 2:59 PM ' Time: 1/1/0001 12:59:00 PM, String: 12:59 PM ' Time: 1/1/0001 10:59:00 AM, String: 10:59 AM |
5. Utils Namespace
The following is the Utils Namespace. Include this in your project to start using!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
' ============================================================================ ' Author: Kenneth Perkins ' Date: Nov 26, 2020 ' Taken From: http://programmingnotes.org/ ' File: Utils.vb ' Description: Handles general utility functions ' ============================================================================ Option Strict On Option Explicit On Namespace Global.Utils Public Module modUtils Public Class DateRange Public Property Time As Date Protected showSeconds As Boolean = False Public Shadows Function ToString(Optional condensed As Boolean = False) As String Dim str = Time.ToShortTimeString If condensed Then str = str.Replace(":00", String.Empty) ElseIf showSeconds Then str = Time.ToString("h:mm:ss tt") End If Return str End Function ''' <summary> ''' Generates a time value list containing the time from 'Starting Hour' ''' to 'Ending Hour' separated by 'Minute Step'. 'Starting Hour' and 'Ending Hour' ''' should be an integer from 0 through 23 representing the hour of the day. ''' </summary> ''' <param name="startingHour">The starting hour from 0 through 23</param> ''' <param name="endingHour">The ending hour from 0 through 23</param> ''' <param name="minuteStep">The minute step</param> ''' <param name="includeFullHour">If going in ascending direction, indicates ''' the results should include the entire 'Ending Hour'. If going in descending ''' direction, indicates the results should include the entire 'Starting Hour'</param> ''' <returns>The time value list from 'Starting Hour' to 'Ending Hour'</returns> Public Shared Function GetTimeRange(startingHour As Integer, endingHour As Integer _ , minuteStep As Double, Optional includeFullHour As Boolean = False) As List(Of DateRange) Dim result = New List(Of DateRange) Dim maxHour = 23 Dim minHour = 0 Dim maxMinute = 59 Dim minMinute = 0 Dim startingMinute = minMinute Dim endingMinute = maxMinute Dim showSeconds = Int(minuteStep) <> minuteStep If minuteStep < 0 Then minuteStep = 1 ' Flip results if going in opposite direction If startingHour > endingHour Then minuteStep *= -1 startingMinute = maxMinute endingMinute = minMinute startingHour = Math.Min(startingHour, maxHour) endingHour = Math.Max(endingHour, minHour) Else startingHour = Math.Max(startingHour, minHour) endingHour = Math.Min(endingHour, maxHour) End If If Not includeFullHour Then startingMinute = 0 endingMinute = 0 End If Dim today = Date.Today Dim startTime = today.SetTime(TimeValue($"{Format(startingHour, "00")}:{Format(startingMinute, "00")}")) Dim endTime = today.SetTime(TimeValue($"{Format(endingHour, "00")}:{Format(endingMinute, "00")}")) Dim currentTime = startTime While If(startingHour < endingHour, currentTime <= endTime, currentTime >= endTime) result.Add(New DateRange With { .Time = TimeValue(currentTime.ToString), .showSeconds = showSeconds }) currentTime = currentTime.AddMinutes(minuteStep) End While Return result End Function End Class ' Appends a date and time value together <Runtime.CompilerServices.Extension()> Public Function SetTime([date] As Date, time As Date) As Date Return [date].Date.Add(time.TimeOfDay) End Function End Module End Namespace ' http://programmingnotes.org/ |
6. More Examples
Below are more examples demonstrating the use of the ‘Utils‘ Namespace. Don’t forget to include the module when running the examples!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
' ============================================================================ ' Author: Kenneth Perkins ' Date: Nov 26, 2020 ' Taken From: http://programmingnotes.org/ ' File: Program.vb ' Description: The following demonstrates the use of the Utils Namespace ' ============================================================================ Option Strict On Option Explicit On Imports System Public Module Program Sub Main(args As String()) Try ' Get an hour range from 12AM - 11PM, with a 15 and half minute interval Dim result = Utils.DateRange.GetTimeRange(0, 23, 15.5) For Each value In result Display($"Time: {value.Time}, String: {value.ToString}") Next Display("") ' Get an hour range from 10PM - 10AM, with a 2 hour interval Dim result2 = Utils.DateRange.GetTimeRange(22, 10, 120) For Each value In result2 Display($"Time: {value.Time}, String: {value.ToString(True)}") Next Display("") ' Get an hour range from 12AM - 11PM, with a 15 and half minute interval Dim result3 = Utils.DateRange.GetTimeRange(0, 23, 15.5, True) For Each value In result3 Display($"Time: {value.Time}, String: {value.ToString}") Next Display("") ' Get an hour range from 10PM - 10AM, with a 2 hour interval Dim result4 = Utils.DateRange.GetTimeRange(22, 10, 120, True) For Each value In result4 Display($"Time: {value.Time}, String: {value.ToString(True)}") Next Catch ex As Exception Display(ex.ToString) Finally Console.ReadLine() End Try End Sub Public Sub Display(message As String) Console.WriteLine(message) Debug.Print(message) End Sub End Module ' http://programmingnotes.org/ |
QUICK NOTES:
The highlighted lines are sections of interest to look out for.
The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.
Leave a Reply