Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2039
  • Last Modified:

Morning, Afternoon and Evening using VB.net

I must have lost hte plot this morning 'cos I can't remember how to do this simple task:)

A web page using vb.net needs to welcome the user with Good Morning, Good Afternoon or Good Evening and place it in Label1.text

Please help

Graham
0
gjpitt
Asked:
gjpitt
  • 4
  • 4
  • 2
  • +2
3 Solutions
 
cyberdevil67Commented:
Hi gjpitt,

   Hmmm, would check now() > 12.000am < 12:00pm then good morning...

   Or if now() >12:00pm and less than 6:00 pm then good afternooon
   or if now() > 6:00 pm and less than 12:00 am then good evening

   not work?

Cheers!
0
 
gjpittAuthor Commented:
That is roughly what I began with but the If statement doesn't like the format of the times

I've tried...

"00:00", "#00:00", "#00:00#"
"00:00:00", "#00:00:00", "#00:00:00#"

             Dim strDaySection As String
            Dim tmpTime As DateTime

            tmpTime = Now()
            If (tmpTime > "00:00" And tmpTime < "18:00") Then
                strDaySection = "Good Morning"
            Else
                If (tmpTime > "12:00" And tmpTime < "18:00") Then
                    strDaySection = "Good Afternoon"
                Else
                    If (tmpTime > "18:00" And tmpTime < "23:59:59") Then
                        strDaySection = "Good Evening"
                    End If
                End If
            End If
            Label1.Text = strDaySection
0
 
jong3Commented:
You are comparing a DateTime with a string... That is causing problems.

You will need to compare either a Datetime with a Datetime,  OR a String with a string.

Try doing this:

tmpTime.ToString("HH:mm")
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
RejojohnyCommented:
   Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim TimeSpend As System.TimeSpan = Now.Subtract("00:00:00")
        Dim HourSpend As Integer = TimeSpend.Hours
        Select Case HourSpend
            Case Is > 12
                Label1.text = ("Good afternoon")
            Case Is > 18
                Label1.text = ("Good Evening")
            Case Else
                Label1.text = ("Good Morning")
        End Select
    End Sub
0
 
cyberdevil67Commented:
Try tmpTime.Hour() to return the hour

and compare that
0
 
gjpittAuthor Commented:
jong3: Valid point and helpful towards solution

Rejojohny: helpful but always produce Good Morning

cyberdevil67:
        Dim tmpTime As DateTime
        tmpTime = Now()

        Select Case tmpTime.Hour
            Case Is > "00"
                Label1.Text = "Good Morning"
            Case Is > "12"
                Label1.Text = "Good Afternoon"
            Case Is > "18"
                Label1.Text = "Good Evening"

        End Select

All: Using Select always chooses Good Morning I think because it is not working through the list of cases but just choosing the first match.  Going to try if..theen..else to se what happens

0
 
cyberdevil67Commented:
No you need to checif its between certain times

 if tmptime > 00 and < 12 then good morning
if timtime >12 and < 6 then good afternoon

etc.
0
 
gjpittAuthor Commented:
This is the solution that works:)  

     Dim tmpTime As Integer
        tmpTime = Now().Hour

        If tmpTime > 0 And tmpTime < 12 Then
            Label1.Text = "Good Morning"
        Else
            If tmpTime > 12 And tmpTime < 18 Then
                Label1.Text = "Good Afternoon"
            Else
                If tmpTime > 18 And tmpTime < 0 Then
                    Label1.Text = "Good Evening"
                End If
            End If
        End If

and as you all contributed I'll splits the points accordingly.

Thanks everyone
Graham
0
 
RejojohnyCommented:
did u change the system time?? else it will always showing good morning .. based on the current system time ... i have tried it and it worked ...
0
 
gjpittAuthor Commented:
My system time should have produced Good Afternoon - it didn't sorry.
0
 
cyberdevil67Commented:
So if you do
MessageBox.Show(tmpTime)

What does this say?
0
 
David FlemingCommented:
Guys,

btw - You do Not Have to have to check between certain times.
All you have to do is reverse order the checking that you had:

Case Is > 18
       'good evening
Case Is > 12
     'good afternoon
Case Else
    'good morning
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 4
  • 4
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now