ken abaki
ken abaki

Reputation: 97

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

Upvotes: 1

Views: 1761

Answers (4)

Quinn
Quinn

Reputation: 4504

This will allow you to define your own 'AM' and 'PM':

' FormatDateTime(Now(), 4) gives 24-hour format (hh:mm)
If Hour(FormatDateTime(Now(), 4)) + Minute(FormatDateTime(Now(), 4))/60 <= 12.5 Then
    ' Is AM
Else
    ' Is PM
End If  

Upvotes: 0

j113203
j113203

Reputation: 11

maybe this way is more effective

MsgBox(DateTime.Now.AddMinutes(-30).ToString("tt"))

Upvotes: -1

nabuchodonossor
nabuchodonossor

Reputation: 2060

sorry when I do not understand your question, but TimeOfDay HAS this Information:

    Dim hour As Date = TimeOfDay
    If hour.Hour >= 12 Then
        ' do whatever you like to do on afternoon
    Else
        ' time to wake up
    End If

had to test this first in my ide

Upvotes: 0

Jens
Jens

Reputation: 6375

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

Upvotes: 3

Related Questions