Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading