Function to test if the current time is within a time window I have written the following code that tests if the current time is within a time window. It seems to work ok with my test cases, but I would be interested if anyone can see any possible problems or has any ideas for improvements?

Public Function IsCurrentTimeBetween(ByRef dteStartTime As DateTime, ByRef dteEndTime As DateTime) As Boolean

    Dim dteStart As DateTime, dteEnd As DateTime, dteCurrent As DateTime

    'strip off any date portion of the dates
    dteStart = Format(dteStartTime, "HH:mm:ss")
    dteEnd = Format(dteEndTime, "HH:mm:ss")
    dteCurrent = Format(DateTime.Now, "HH:mm:ss")

    If dteEnd &lt; dteStart Then
        'the times span midnight
        If dteCurrent &lt; dteStart And dteCurrent &lt; dteEnd Then
            dteCurrent = dteCurrent.AddDays(1)
        End If
        dteEnd = dteEnd.AddDays(1)
    End If

    Return (dteStart &lt;= dteCurrent AndAlso dteCurrent &lt;= dteEnd)

End Function


Usage:

'Work out if we are between 5 to midnight and quarter past midnight    
Debug.WriteLine("Is in midnight window? " + IsCurrentTimeBetween("23:55", "00:15").ToString)
You are taking a DateTime, formatting it to a string, then assigning that to another datetime, just to get the time of day. That won't compile with Option Strict On. What you want is provided by DateTime.TimeOfDay.

I would also change ByRef to ByVal in the parameter list.

Once you are comparing timespan instead of date, you don't have to do date addition. You can just detect the wrap-around case and you only have to check endTime.

 Public Function IsCurrentTimeBetween(ByVal dteStartTime As DateTime, ByVal dteEndTime As DateTime) As Boolean

            Dim startTime, endTime, currentTime As TimeSpan

            startTime = dteStartTime.TimeOfDay
            endTime = dteEndTime.TimeOfDay
            currentTime = Now.TimeOfDay

            If endTime &lt; startTime Then
                'the times span midnight     
                Return (currentTime &lt;= endTime)
            Else
                'the times do not span midnight
                Return (startTime &lt;= currentTime AndAlso currentTime &lt;= endTime)
            End If

End Function