Hey,

Im trying for a message box to pop up with a price, depending on the time. Before 11:30 costs £3.00, afterwards costs £4.50. This is what i have tried but it this code gives me the message for £3.00 when the time is over 11:30

Dim day As String = Format(Today, "dddd")
        Dim time As Date

        time = DateTime.Now.ToString("hh:mm:ss")

        Select Case day
            Case "Sunday"
                If time > "11:30:00" And time < "23:59:59" Then
                    MsgBox("£4.50 Please")
                Else
                    MsgBox("Coffee Morning:" & vbCr & "£3.00 Please")
                End If
            Case "Monday"
            Case "Tuesday"
            Case "Wednesday"
            Case "Thursday"
            Case "Friday"
            Case "Saturday"
        End Select

Any help?

Thank you !

Recommended Answers

All 7 Replies

There's no reason to convert a datetime value to a string. Use Hour and Minute properties instead

Dim day As String = Format(Today, "dddd")
Dim time As Date
Dim CurrHour As Integer
Dim CurrMinute As Integer

time = DateTime.Now

CurrHour = time.Hour ' Get current hour value
CurrMinute = time.Minute ' Get current minute value

Select Case day
  Case "Sunday"
    If CurrHour >= 11 AndAlso CurrMinute >= 30 Then
      MsgBox("£4.50 Please")
    Else
      MsgBox("Coffee Morning:" & vbCr & "£3.00 Please")
    End If
  Case "Monday"
  Case "Tuesday"
  Case "Wednesday"
  Case "Thursday"
  Case "Friday"
  Case "Saturday"
End Select

Hey, thank you that worked perfectly, thank you so much :)

Hi! Nice to hear that you got answer to your problem, could you please mark the thread as solved. Thank you!

Surly after 12 that would not work between 12:00 and 12:29 and 13:00 and 13:29 as the first part would be correct but the secound part of the if stament would only be correct for the secound half of every hour??

the andalso basicly means it will only attempt to run the secound part of the if (the bit after the andalso) ONLY if the first part is true... for example you may want to do an AND where the first part check for a null value then the secoun part (if the first part is not null) can complete.

Surly after 12 that would not work between 12:00 and 12:29 and 13:00 and 13:29 as the first part would be correct but the secound part of the if stament would only be correct for the secound half of every hour??

The andalso basicly means it will only attempt to run the secound part of the if (the bit after the andalso) ONLY if the first part is true... for example you may want to do an AND where the first part check for a null value then the secoun part (if the first part is not null) can complete.

Surly after 12 that would not work between 12:00 and 12:29 and 13:00 and 13:29 as the first part would be correct but the secound part of the if stament would only be correct for the secound half of every hour??

Correct. My answer had a "minor" bug but I believe the OP noticed and fixed it ;)

The andalso basicly means it will only attempt to run the secound part of the if (the bit after the andalso) ONLY if the first part is true... for example you may want to do an AND where the first part check for a null value then the secoun part (if the first part is not null) can complete.

I suggest using AndAlso (and OrElse) if you have a good reason to short circuit the evaluation. For example, the following code works even if myStr is an empty string (it short circuits)

Dim myStr As String
...
If Not String.IsNullOrEmpty(myStr) AndAlso myStr.Length > 3 Then
  ' Do domething
End If

but

Dim myStr As String
...
If Not String.IsNullOrEmpty(myStr) And myStr.Length > 3 Then
  ' Do domething
End If

would cause a null reference error with an empty string.

Dim day As String = Format(Today, "dddd")
Dim time As Date
Dim CurrHour As Integer
Dim CurrMinute As Integer

time = DateTime.Now

CurrHour = time.Hour ' Get current hour value
CurrMinute = time.Minute ' Get current minute value

Select Case day
  Case "Sunday"
    If CurrHour >= 11 AndAlso CurrMinute >= 30 Then
      MsgBox("£4.50 Please")
    Else
      MsgBox("Coffee Morning:" & vbCr & "£3.00 Please")
    End If
  Case "Monday"
  Case "Tuesday"
  Case "Wednesday"
  Case "Thursday"
  Case "Friday"
  Case "Saturday"
End Select

OK I don't see the OP adjustment but basicly line 13 wont work
as i said after 12:00 the if wont become true and the price will change back too £3.00

separating hours and minuets like that will NOT work in this instance what you need to do is TOTAL minuets
example

If Now.TimeOfDay.TotalMinutes >= 690 Then
MsgBox("£4.50 Please")

690 is the total minutes passed since 00:00 which is 11:30 (11 hours plus 30 minuets) or (11*60+30=690)

Hope this clarifies that I ment :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.