Question: TimeSpan

  • Thread starter Thread starter VB Programmer
  • Start date Start date
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!
 
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
 
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
 
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
 
Back
Top