Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 298
  • Last Modified:

Finding a space between 2 times of day in VB.NET

I'm trying to do something between 2 times of day. The following works fine...

dim StopTime, StopTime2 as DateTime
Dim StopDuration as integer

StopTime = ConfigurationSettings.AppSettings("StopTime")
StopDuration = ConfigurationSettings.AppSettings("StopDuration")
StopTime2 = StopTime.AddHours(StopDuration)

If Now > StopTime Then
   MsgBox("got it")
End If

Open in new window


However, when I try to add in a 2nd comparison, it skips over my If/Then. Stepping through, the times look ok.


dim StopTime, StopTime2 as DateTime
Dim StopDuration as integer

StopTime = ConfigurationSettings.AppSettings("StopTime")
StopDuration = ConfigurationSettings.AppSettings("StopDuration")
StopTime2 = StopTime.AddHours(StopDuration)

If Now > StopTime And Now < StopTime2 Then
   MsgBox("got it")
End If

Open in new window


What am I doing wrong here?
0
Mike Miller
Asked:
Mike Miller
  • 3
  • 3
1 Solution
 
Todd GerbertIT ConsultantCommented:

1

StopTime and StopTime2 are a little confusing/misleading as variable names - maybe startTime and stopTime make more sense? ;)

2

Since DateTime's contain both date and time data, it's possible that the date part is causing your grief; e.g. If StopTime is January 1st in the year 3050 then "Now" will never be greater than StopTime. Try comparing the TimeOfDay properties of your DateTime's, so the date part won't be an issue.
If Now.TimeOfDay > startTime.TimeOfDay And Now.TimeOfDay < stopTime.TimeOfDay Then ...

Open in new window

0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
What is actually returned by ConfigurationSettings.AppSettings("StopTime")?
0
 
Todd GerbertIT ConsultantCommented:
Hmm, I'm curious to know if that actually answered your question, or did you find some other answer and just randomly accept a comment as the solution? ;)
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Mike TomlinsonMiddle School Assistant TeacherCommented:
That probably won't work if your time interval SPANS MIDNIGHT....
0
 
Todd GerbertIT ConsultantCommented:
That's true, I guess the Date part would be of some importance in that case. Which brings us full circle back to your question,
What is actually returned by ConfigurationSettings.AppSettings("StopTime")?
, and mine,
...I'm curious to know if that actually answered your question...?
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Exactly.  =)

Just for the sake of others possibly reading this thread in the future...

Let's assume that a STRING representing a 24-hour time is returned.

This is one way to properly handle that scenario:
        Dim StopTime As String
        Dim StopDuration As Integer

        StopTime = "22:15" ' 10:15 pm
        StopDuration = 3 ' hours

        Dim dt As DateTime
        Dim AllowedFormats() As String = {"H:m", "HH:m", "H:mm", "HH:mm"}
        If DateTime.TryParseExact(StopTime, AllowedFormats, Nothing, Globalization.DateTimeStyles.None, dt) Then
            Dim dtA, dtB As DateTime
            dtA = DateTime.Today.Add(dt.TimeOfDay)
            dtB = dtA.AddHours(StopDuration)

            Debug.Print("dtA = " & dtA)
            Debug.Print("dtB = " & dtB)

            If Now > dtA And Now < dtB Then
                MessageBox.Show("In the time window!")
            End If
        End If

Open in new window

0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now