views:

601

answers:

4

What is the best method to determine if the current time is AM or PM using VB.NET?

Currently I'm using If Date.Today.ToString.Contains("AM") but I'm sure there is a better method.

Good <%If Date.Today.ToString.Contains("AM") Then Response.Write("Morning") Else Response.Write("Afternoon")%>
+6  A: 

If Date.Now.Hour < 12 Then ... perhaps?

Lasse V. Karlsen
Does Date.Now.Hour always return 0-23, or could it ever return 1-12?
Bill the Lizard
It will always return 0-23. Displaying it, thus converting it to a string, might show 0-11/12, etc. but that's a localization/display issue. The Time property will always be 00:00:00 - 23:59:59 (+ milliseconds etc.)
Lasse V. Karlsen
See here: http://msdn.microsoft.com/en-us/library/system.datetime.hour.aspx In the remarks it says this: The value of the Hour property is always expressed using a 24-hour clock.
Lasse V. Karlsen
+2  A: 

Hello,

How about this:

Now.Hour > 17 ' Night Now.Hour < 5 ' Day

HTH,

Colby Africa

Colby Africa
+1 for making the time zone issue stick out.
Peter LaComb Jr.
A: 

Now.ToString("t") == "A" //or //"P"

Jacob Adams
+1  A: 

How about this:

dim hours as int = DateTime.Now.Hour
dim str as string
if hours < 12 Then
    str = "Morning"
else if hours < 17 then
    str = "Afternoon"
else if hours > 17 then
    str = "Evening"
End If
Eppz
And hours = 17 then str = "tea time"
Drejc