Results 1 to 6 of 6

Thread: VB.NET: Check Date, Inputs from different comboBox...[Resolved]

  1. #1

    Thread Starter
    Addicted Member toytoy's Avatar
    Join Date
    Jul 2004
    Posts
    230

    VB.NET: Check Date, Inputs from different comboBox...[Resolved]

    Say i have 2 date...

    How to check if one is greater than the other...

    For Example:

    1/11/2004 > 28/ 10/2004
    28/11/2004 > 27/10/2004
    27/11/2004 > 27/11/2003

    Thanks
    Last edited by toytoy; Oct 20th, 2004 at 05:30 AM.

  2. #2
    Frenzied Member Asgorath's Avatar
    Join Date
    Sep 2004
    Location
    Saturn
    Posts
    2,036
    Hi

    You can use the DateTime.CompareTo method.

    Regards
    Jorge
    "The dark side clouds everything. Impossible to see the future is."

  3. #3

    Thread Starter
    Addicted Member toytoy's Avatar
    Join Date
    Jul 2004
    Posts
    230
    forget to add in some detail....

    Start Date End Date
    1/11/2004 > 28/ 10/2004 'Invalid Date
    28/11/2004 > 27/10/2004 'Invalid Date
    27/11/2004 > 27/11/2003 'Invalid Date
    26/11/2004 < 27/11/2004 'Valid Date

    It is not system date but the selection date between start and end...

    Is it still use the DateTime.CompareTo since i want to compare only the date not time..

    Thanks

  4. #4

    Thread Starter
    Addicted Member toytoy's Avatar
    Join Date
    Jul 2004
    Posts
    230
    Say that date is choosen by every combo box...

    Start Day by one combo, month by another combo , year by textbox..
    End Day got another set of that combo box and textbox..

    which means all the date is not known until runtime when the user select from every combo box..

    My Coding:
    Code:
    Dim DateState As Integer = Date.Compare(Date.FromOADate("(cboSDay.SelectedIndex + 1) & " / " & (cboSMonth.SelectedIndex + 1 )& " / " & (cboSYear.Text)"), Date.FromOADate("(cboEDay.SelectedIndex + 1) & " / " & (cboEMonth.SelectedIndex + 1) & " / " & (cboEYear.Text)")) 
    
    MessageBox.Show("Date are Equal") 
    ElseIf DateState < 0 Then 
    MessageBox.Show("Date 1 is greater than Date 2") 
    Else 
    MessageBox.Show("Date 2 is greater than Date 1") 
    End If
    Error Message:
    Code:
    Additional information: Cast from string "(cboSDay.SelectedIndex + 1) & " to type 'Double' is not valid.
    Does anyone know how to correct the error...

    Thanks
    Last edited by toytoy; Dec 3rd, 2004 at 07:56 AM.

  5. #5

    Thread Starter
    Addicted Member toytoy's Avatar
    Join Date
    Jul 2004
    Posts
    230
    finally, solve it...

    I used the subtract method instead of DateTime.CompareTo method...

    This is my coding:
    Code:
    Dim DateState As Integer = CDate((cboSMonth.SelectedIndex + 1) & " / " & (cboSDay.SelectedIndex + 1) & " / " & (cboSYear.Text)).Subtract(CDate((cboEMonth.SelectedIndex + 1) & " / " & (cboEDay.SelectedIndex + 1) & " / " & (cboEYear.Text))).TotalDays 
    
    If DateState = 0 Then 
    MessageBox.Show("Date are equal") 
    ElseIf DateState < 0 Then 
    MessageBox.Show("Date 1 is greater than Date 2") 
    Else 
    MessageBox.Show("Date 2 is greater than Date 1") 
    End If
    Last edited by toytoy; Dec 3rd, 2004 at 07:56 AM.

  6. #6

    Thread Starter
    Addicted Member toytoy's Avatar
    Join Date
    Jul 2004
    Posts
    230
    Another method which i get it from other forums using DateTime.CompareTo method...

    Coding:
    Code:
    Dim startDate As DateTime = DateTime.Parse(String.Format("{0}/{1}/{2}", cboSMonth.Text, cboSDay.Text, cboSYear.Text))
    
    Dim endDate As DateTime = DateTime.Parse(String.Format("{0}/{1}/{2}", cboEMonth.Text, cboEDay.Text, cboEYear.Text))
    
    Select Case DateTime.Compare(startDate, endDate)
    Case -1
    ' smaller
    MessageBox.Show("Date 2 is greater than Date 1")
    Case 1
    ' bigger
    MessageBox.Show("Date 1 is greater than Date 2")
    Case 0
    ' same
    MessageBox.Show("Date are Equal")
    End Select
    Last edited by toytoy; Dec 3rd, 2004 at 07:57 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width