Question: TimeSpan

V

VB Programmer

I want my web page to take the current time and either say "Good
Morning/Afternoon or Evening". Any ideas how I can do this? I tried this
but it's taking the times as strings...

Dim strMyTime As String = Now.ToShortTimeString

If strMyTime < "11:30 AM" Then
Response.Write("Good Morning")
ElseIf strMyTime > "11:30 AM" And strMyTime < "4:30 PM" Then
Response.Write("Good Afternoon")
Else
Response.Write("Good Evening")
End If

I'm sure I can use TimeSpan.Compare but not sure how to proceed. Any ideas?

Thanks!
 
R

Roger Helliwell

Dim strMyTime As String = Now.ToShortTimeString

If strMyTime < "11:30 AM" Then
Response.Write("Good Morning")
ElseIf strMyTime > "11:30 AM" And strMyTime < "4:30 PM" Then
Response.Write("Good Afternoon")
Else
Response.Write("Good Evening")
End If

You are comparing the actual strings rather than the time they
represent. Instead use,

Dim dtMyTime As DateTime = Now

then you can use the CompareTo() function on each time, or you could
even use the TimeSpan() function. There are good examples in the MSDN
docs for the DateTime class.

Roger
 
V

VB Programmer

This works:

Dim iHour As Integer = Now.Hour
If iHour <= 11 Then

Response.Write("Good Morning")

ElseIf iHour <= 18 Then

Response.Write("Good Afternoon")

Else

Response.Write("Good Evening")

End If
 
H

Hans Kesting

VB Programmer said:
This works:

Dim iHour As Integer = Now.Hour
If iHour <= 11 Then

Response.Write("Good Morning")

ElseIf iHour <= 18 Then

Response.Write("Good Afternoon")

Else

Response.Write("Good Evening")

End If

Just two small notes:
* at 2 in the morning it will say "Good Morning" (I leave the modification to the reader)
* if the user is not in the same timezone as the server, the text will seem off.

Hans Kesting
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top