vb.nettimetimeofday

how to know am or pm in vb


im doing my activity dtr system and i want to know how to determine whether it am or pm? 7am - 12:30 pm should go on AM, and 1pm - 5PM will go to pm.

for example:

if the time is 12:30 pm its still go to am and if its 12:31 pm it will go now to pm.

Dim hour As Date = TimeOfDay.ToString("hh:mm")
    If hour <= "12:30" Then
        isTimeAM_Exist() ' will go to am
    Else
        isTimePM_Exist() ' will go to pm
    End If

in my code 4:00 pm still go in am.

thankyou in advance


Solution

  • Instead of converting to string, work directly with the DateTime structure, and compare it with a correct starting value

    Dim now = Date.Now
    If now < New DateTime(now.Year, now.Month, now.Day, 12, 00, 0) Then
        'It's currently AM
    Else
        'It's currently PM
    End If