Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7911339
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T13:12:31+00:00 2026-06-03T13:12:31+00:00

I implmented a function to check time range in vb.net. But it is giving

  • 0

I implmented a function to check time range in vb.net. But it is giving me wrong output.
My starttime is everyday at 11.00 Pm and end Time is 5.00 AM. My function if i pass 1.10 AM does not return me true output as this falls under that time range. Not sure what iam doing wrong.

Private Function CheckTimeRange() As Boolean
  Dim retValue As Boolean = True
  Try
    Dim Dt As DateTime = DateTime.Now
    Dim StartDt As DateTime = Convert.ToDateTime("11.00 PM")
    Dim EndDt As DateTime =  Convert.ToDateTime("5.00 AM")

    Dim startTime As New TimeSpan(StartDt.Hour, StartDt.Minute, 0)
    Dim endTime As New TimeSpan(EndDt.Hour, EndDt.Minute, 0)
    Dim now As TimeSpan = DateTime.Now.TimeOfDay

    If (now > startTime) AndAlso (now < endTime) Then
      retValue = True
    Else
      retValue = False
    End If
    Return retValue
  Catch ex As Exception
  End Try
End Function
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T13:12:33+00:00Added an answer on June 3, 2026 at 1:12 pm

    I think you’re overcomplicating your code. You could do:

    Private Function CheckTimeRange() As Boolean
        Return DateTime.Now.Hour >= 23 OrElse DateTime.Now.Hour < 5
    End Function
    

    Edit:

    If the start and end times are entered by the user, you should first convert the string values into TimeSpan objects, then you could use a more flexible method which takes date, min time and max time as parameters:

    Private Function CheckTimeRange(myDate As DateTime, minTime as TimeSpan, maxTime As TimeSpan) As Boolean
        If minTime > maxTime Then
            Return myDate.TimeOfDay >= minTime OrElse myDate.TimeOfDay < maxTime
        Else
            Return myDate.TimeOfDay >= minTime AndAlso myDate.TimeOfDay < maxTime
        End If
    End Function
    

    Example usage:

    Dim minTime As New TimeSpan(23, 0, 0) 'Should be converted from 11.00 PM
    Dim maxTime As New TimeSpan(5, 0, 0) 'Should be converted from 5.00 AM
    CheckTimeRange(New Date(2012, 1, 1, 15, 0, 0), minTime, maxTime) '==> false
    CheckTimeRange(New Date(2012, 1, 1, 22, 30, 0), minTime, maxTime) '==> false
    CheckTimeRange(New Date(2012, 1, 1, 23, 00, 0), minTime, maxTime) '==> true
    CheckTimeRange(New Date(2012, 1, 2, 1, 10, 0), minTime, maxTime) '==> true
    CheckTimeRange(New Date(2012, 1, 2, 4, 59, 0), minTime, maxTime) '==> true
    CheckTimeRange(New Date(2012, 1, 2, 5, 10, 0), minTime, maxTime) '==> false
    

    If you have trouble converting string values to TimeSpan you should ask a new question for this specific task.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First, check out this document first: http://htmlcleaner.sourceforge.net/doc/org/htmlcleaner/TagNode.html It have a function called: getAllElementsList(boolean isRecursive).
It is usually said callbacks are implemented with function pointers. When I check PortAudio's
I'm searching for a function (perhaps there is one in the .Net-Framework) which I
Is there a good reason why the check function in the Contol.Concurent.STM library has
I'm trying to write a function to check for type equality, ignoring instance variables
Is there any already implemented function that has initialArray, sizeOfInitialArray, outArray, and sizeOfOutArray parameters,
as the question states. I have implemented a function wherein it fetches multiple rows
Since this function is implemented in IE8, I wanted to see exactly what I
How can the following function be implemented in various languages? Calculate the (x,y) point
Two function should be implemented, and I am wondering what type of validation I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.