dcsimg
Results 1 to 7 of 7

Thread: Determine if Now.TimeOfDay Is Between Two Times

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Oct 2003
    Posts
    79

    Determine if Now.TimeOfDay Is Between Two Times

    Hey Guys,

    I want to make a function that determines if Now.TimeOfDay is between two times?

    Basically I want to make a function to determine if the current time is between 6AM and 6PM, and return True if it is and False if its not.

    I've been having a hard time doing the comparison. Any help would be appreciated.

    Thx.

  2. #2
    Super Moderator Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Determine if Now.TimeOfDay Is Between Two Times

    Code:
      Public Function IsDayTime() As Boolean
        Return DateTime.Now.TimeOfDay >= New TimeSpan(6, 0, 0) AndAlso DateTime.Now.TimeOfDay <= New TimeSpan(18, 0, 0)
      End Function
    Joacim Andersson
    If anyone's answer has helped you, please show your appreciation by rating that answer.
    I'd rather run ScriptBrix...
    Joacim's view on stuff.

    MVP

  3. #3
    Super Moderator Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,335

    Re: Determine if Now.TimeOfDay Is Between Two Times

    Quote Originally Posted by Joacim Andersson
    Code:
      Public Function IsDayTime() As Boolean
        Return DateTime.Now.TimeOfDay >= New TimeSpan(6, 0, 0) AndAlso DateTime.Now.TimeOfDay <= New TimeSpan(18, 0, 0)
      End Function
    How would you add two parameters to the function and pass the begin/end time that you wanted to check, and feed that into your function with the TimeSpan thing?
    Please use [Code]your code goes in here[/Code] tags when posting code.
    When you have received an answer to your question, please mark it as resolved using the Thread Tools menu.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    I dont answer coding questions via PM or EMail. Please post a thread in the appropriate forum section.

    Creating A Wizard In VB.NET
    Paging A Recordset
    What is wrong with using On Error Resume Next
    Good Article: Language Enhancements In Visual Basic 2010
    Upgrading VB6 Code To VB.NET
    Microsoft MVP 2005/2006/2007/2008/2009/2010/2011/2012/Defrocked

  4. #4
    Super Moderator Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Determine if Now.TimeOfDay Is Between Two Times

    Code:
      Public Function IsDayTime(ByVal startTime As DateTime, ByVal endTime As DateTime) As Boolean
        Return DateTime.Now.TimeOfDay >= startTime.TimeOfDay AndAlso DateTime.Now.TimeOfDay <= endTime.TimeOfDay
      End Function
    Joacim Andersson
    If anyone's answer has helped you, please show your appreciation by rating that answer.
    I'd rather run ScriptBrix...
    Joacim's view on stuff.

    MVP

  5. #5
    Super Moderator Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,335

    Re: Determine if Now.TimeOfDay Is Between Two Times

    Ah......I had something like that going but I was trying to cram it into the TimeSpan thing, and that is where I was screwing myself up.

    Thanks!
    Please use [Code]your code goes in here[/Code] tags when posting code.
    When you have received an answer to your question, please mark it as resolved using the Thread Tools menu.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    I dont answer coding questions via PM or EMail. Please post a thread in the appropriate forum section.

    Creating A Wizard In VB.NET
    Paging A Recordset
    What is wrong with using On Error Resume Next
    Good Article: Language Enhancements In Visual Basic 2010
    Upgrading VB6 Code To VB.NET
    Microsoft MVP 2005/2006/2007/2008/2009/2010/2011/2012/Defrocked

  6. #6
    Super Moderator Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Determine if Now.TimeOfDay Is Between Two Times

    Well TimeOfDay returns a TimeSpan. You could of course do it like this as well:
    Code:
      Public Function IsDayTime(ByVal startTime As DateTime, ByVal endTime As DateTime) As Boolean
        Return DateTime.Now.TimeOfDay >= New TimeSpan(startDate.Ticks) AndAlso DateTime.Now.TimeOfDay <= New TimeSpan(endTime.Ticks)
      End Function
    or if you want to be complicated:
    Code:
      Public Function IsDayTime(ByVal startTime As DateTime, ByVal endTime As DateTime) As Boolean
        Return DateTime.Now.TimeOfDay >= New TimeSpan(startTime.Hours, startTime.Minutes, startTime.Seconds) AndAlso ...
      End Function
    Joacim Andersson
    If anyone's answer has helped you, please show your appreciation by rating that answer.
    I'd rather run ScriptBrix...
    Joacim's view on stuff.

    MVP

  7. #7
    Super Moderator Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,335

    Re: Determine if Now.TimeOfDay Is Between Two Times

    Nah, thats OK. The first way works just fine.
    Please use [Code]your code goes in here[/Code] tags when posting code.
    When you have received an answer to your question, please mark it as resolved using the Thread Tools menu.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    I dont answer coding questions via PM or EMail. Please post a thread in the appropriate forum section.

    Creating A Wizard In VB.NET
    Paging A Recordset
    What is wrong with using On Error Resume Next
    Good Article: Language Enhancements In Visual Basic 2010
    Upgrading VB6 Code To VB.NET
    Microsoft MVP 2005/2006/2007/2008/2009/2010/2011/2012/Defrocked

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width