Results 1 to 4 of 4

Thread: Finding Max and Min of given date

  1. #1

    Thread Starter
    Member
    Join Date
    May 2005
    Posts
    45

    Finding Max and Min of given date

    Hi There

    the given day and month as : 11/02, 29/12, 19/05, 31/09, 29/11 (i need only day and Month)

    How to find the Max and Min of Day and Month in Visual Basic. I am new in this field. Any one can help in this issue.

    thanx in advance.

    by
    yasin

  2. #2
    PowerPoster lintz's Avatar
    Join Date
    Mar 2003
    Location
    The 19th Hole
    Posts
    2,697

    Re: Finding Max and Min of given date

    This will work. HTH

    VB Code:
    1. Dim AllDates(4) As String
    2. Dim MinDay As Integer
    3. Dim MaxDay As Integer
    4. Dim MinMonth As Integer
    5. Dim MaxMonth As Integer
    6. Dim i As Integer
    7.  
    8. '11/02, 29/12, 19/05, 31/09, 29/11
    9. 'BTW = September only has 30 days :) so 31/09 is an invalid date
    10. AllDates(0) = "11/02"
    11. AllDates(1) = "29/12"
    12. AllDates(2) = "19/05"
    13. AllDates(3) = "30/09"
    14. AllDates(4) = "29/11"
    15.  
    16. For i = 0 To UBound(AllDates())
    17.  
    18.     'Check for days first
    19.     If Left$(AllDates(i), 2) > MaxDay Or MaxDay = 0 Then
    20.     MaxDay = Left$(AllDates(i), 2)
    21.     End If
    22.    
    23.     If Left$(AllDates(i), 2) < MinDay Or MinDay = 0 Then
    24.     MinDay = Left$(AllDates(i), 2)
    25.     End If
    26.    
    27.     'Check for months next
    28.     If Right$(AllDates(i), 2) > MaxMonth Or MaxMonth = 0 Then
    29.     MaxMonth = Right$(AllDates(i), 2)
    30.     End If
    31.    
    32.     If Right$(AllDates(i), 2) < MinMonth Or MinMonth = 0 Then
    33.     MinMonth = Right$(AllDates(i), 2)
    34.    
    35.     End If
    36.  
    37. Next
    38.  
    39. Debug.Print "MinDay ="; MinDay; ", MaxDay = "; MaxDay; ", MinMonth = "; MinMonth; ", MaxMonth = "; MaxMonth; ""

  3. #3
    Lively Member Hojo's Avatar
    Join Date
    Jul 2005
    Location
    Brisbane, Australia
    Posts
    119

    Re: Finding Max and Min of given date

    If what you werwe wanting was the min date and max date (i.e. not min day min moth max day max month) The you could use the dateValue() function to check.

    VB Code:
    1. Option Explicit
    2.  
    3.     Dim MinDate As String 'Variable to store the earliest date
    4.     Dim MaxDate As String 'Variable to store latest date
    5.    
    6. Private Sub Command1_Click()
    7.  
    8.     Dim Date1 As String
    9.     Dim Date2 As String
    10.  
    11.     MinDate = "31/12" 'Set to highest date to check against inputed dates
    12.     MaxDate = "1/1"   'Set to lowest date to check against inputed dates
    13.    
    14.     Date1 = "3/11"
    15.     Date2 = "22/12"
    16.    
    17.     CheckDateOrder (Date1)
    18.     CheckDateOrder (Date2)
    19.    
    20.     Label1.Caption = MinDate
    21.     Label2.Caption = MaxDate
    22.    
    23.    
    24. End Sub
    25.  
    26. Private Sub CheckDateOrder(iDate)
    27.  
    28.    
    29.     If DateValue(iDate) < DateValue(MinDate) Then
    30.    
    31.         MinDate = iDate
    32.    
    33.     End If
    34.  
    35.     If DateValue(iDate) > DateValue(MaxDate) Then
    36.    
    37.         MaxDate = iDate
    38.    
    39.     End If
    40.  
    41. End Sub

    Hope this helps,

    Cheers
    Despite body and mind, my youth will never die!

    Everytime I learn something new it pushes some old stuff out of my brain!

  4. #4
    Member kryptonboy22's Avatar
    Join Date
    May 2006
    Location
    philippines
    Posts
    48

    Re: Finding Max and Min of given date

    if your date is stored in a database its easy to use a query
    VB Code:
    1. SELECT Max(datefield) FROM Table

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