|
-
Jun 15th, 2006, 05:27 AM
#1
Thread Starter
Member
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
-
Jun 15th, 2006, 06:14 AM
#2
Re: Finding Max and Min of given date
This will work. HTH
VB Code:
Dim AllDates(4) As String
Dim MinDay As Integer
Dim MaxDay As Integer
Dim MinMonth As Integer
Dim MaxMonth As Integer
Dim i As Integer
'11/02, 29/12, 19/05, 31/09, 29/11
'BTW = September only has 30 days :) so 31/09 is an invalid date
AllDates(0) = "11/02"
AllDates(1) = "29/12"
AllDates(2) = "19/05"
AllDates(3) = "30/09"
AllDates(4) = "29/11"
For i = 0 To UBound(AllDates())
'Check for days first
If Left$(AllDates(i), 2) > MaxDay Or MaxDay = 0 Then
MaxDay = Left$(AllDates(i), 2)
End If
If Left$(AllDates(i), 2) < MinDay Or MinDay = 0 Then
MinDay = Left$(AllDates(i), 2)
End If
'Check for months next
If Right$(AllDates(i), 2) > MaxMonth Or MaxMonth = 0 Then
MaxMonth = Right$(AllDates(i), 2)
End If
If Right$(AllDates(i), 2) < MinMonth Or MinMonth = 0 Then
MinMonth = Right$(AllDates(i), 2)
End If
Next
Debug.Print "MinDay ="; MinDay; ", MaxDay = "; MaxDay; ", MinMonth = "; MinMonth; ", MaxMonth = "; MaxMonth; ""
-
Jun 15th, 2006, 07:07 PM
#3
Lively Member
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:
Option Explicit
Dim MinDate As String 'Variable to store the earliest date
Dim MaxDate As String 'Variable to store latest date
Private Sub Command1_Click()
Dim Date1 As String
Dim Date2 As String
MinDate = "31/12" 'Set to highest date to check against inputed dates
MaxDate = "1/1" 'Set to lowest date to check against inputed dates
Date1 = "3/11"
Date2 = "22/12"
CheckDateOrder (Date1)
CheckDateOrder (Date2)
Label1.Caption = MinDate
Label2.Caption = MaxDate
End Sub
Private Sub CheckDateOrder(iDate)
If DateValue(iDate) < DateValue(MinDate) Then
MinDate = iDate
End If
If DateValue(iDate) > DateValue(MaxDate) Then
MaxDate = iDate
End If
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!
-
Jun 15th, 2006, 10:23 PM
#4
Member
Re: Finding Max and Min of given date
if your date is stored in a database its easy to use a query
VB Code:
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|