Home > Mobile >  how do i make a if statement for AM/PM like < 12 am >=12 pm
how do i make a if statement for AM/PM like < 12 am >=12 pm

Time:11-14

i want to make am/pm if statement in vb.net and i dont know how to do it

 If txtHourEnter.Text < "12" Then
            lblTimeIn.Text = txtHourEnter.Text   ":"   txtMinEnter.Text   "AM"
        ElseIf txtHourEnter.Text >= "12" Then
            lblTimeIn.Text = txtHourEnter.Text   ":"   txtMinEnter.Text   "PM"
        End If

i tried this but whenever i type 2 it says pm and if i type 1 it say am

CodePudding user response:

Don't try to work directly with the Text property from controls like that. It just makes calculations hard. Always convert the text to a data type you can compute with, then do the computation, and then finally convert back to something you can display.

Try this:

Dim entered As String = txtHourEnter.Text   ":"   txtMinEnter.Text

Dim time As DateTime
Dim display As String = "Invalid entry"
If DateTime.TryParse(entered, time) Then
    display = time.ToString("h:mm tt")
End If

lblTimeIn.Text = display
  • Related