Reputation: 74094
I would like to code a function that return True if 6 months have passed since a given date (so, I'm on the first day of the seventh month).
Function PassedSixMonthsSince(Dim dGivenDate as Date, Dim dCurrentDate as Date) as Boolean
These are some tests that this function must pass:
Debug.Assert PassedSixMonthsSince("2000-01-01","2000-07-01")=True
Debug.Assert PassedSixMonthsSince("2000-01-31","2000-07-31")=True
Debug.Assert PassedSixMonthsSince("2000-08-31","2001-02-28")=False
Debug.Assert PassedSixMonthsSince("2007-08-31","2008-02-29")=False
Debug.Assert PassedSixMonthsSince("2000-05-31","2000-11-30")=False
I've tried to use DateAdd("m",6,dGivenDate)
, but it's not so easy.
Upvotes: 2
Views: 1455
Reputation: 74094
I have resolved with this code:
Function PassedSixMonthsSince(ByVal dGivenDate as Date,ByVal dCurrentDate as Date)
dGivenDateWithSixMonthsAdded=DateAdd("m", 6, dGivenDate)
if Day(dGivenDate)<>Day(dGivenDateWithSixMonthsAdded) then
dGivenDateWithSixMonthsAdded=DateAdd("d", 1,dGivenDateWithSixMonthsAdded)
end if
PassedSixMonthsSince = (dCurrentDate >= dGivenDateWithSixMonthsAdded)
End Function
Upvotes: 0
Reputation: 700312
DataAdd is the right way to go. You can either add six months to the given date:
PassedSixMonthsSince = DateAdd("m", 6, dGivenDate) >= dCurrentDate
or you can subtract six months from the current date:
PassedSixMonthsSince = dGivenDate >= DateAdd("m", -6, dCurrentDate)
If you don't get the right result, then it's because the dates that you have written can't be parsed correctly. The format you have used is (AFAIK) your own, it's not in use anywere else. The standardised format (ISO 8601) is "2000-02-29"
, and there are other common formats like "2/29/2000"
and "29/2/2000"
. If you want to use date literals instead of parsing strings, they are delimited by hash signs: #2000-02-29#
.
Upvotes: 1
Reputation: 57946
I don't have VB6 installed anymore, but I tried this VBScript code:
<script language="vbscript">
Function PassedSixMonthsSince(dGivenDate, dCurrentDate)
'' // should be just greater, not greater or equals
PassedSixMonthsSince = _
CDate(dCurrentDate) >= DateAdd("m", 6, CDate(dGivenDate))
MsgBox _
"Is " & CDate(dCurrentDate) & _
" greater or equal than " & DateAdd("m", 6, CDate(dGivenDate)) & _
" ? = " & PassedSixMonthsSince
End Function
PassedSixMonthsSince "2000-01-01", "2000-07-01" ''// exactly 6 months
PassedSixMonthsSince "2000-01-31", "2000-07-31" ''// exactly 6 months
PassedSixMonthsSince "2000-08-31", "2001-02-28" ''// One day less 6 months
PassedSixMonthsSince "2000-05-31", "2001-11-30" ''// One day less 6 months
</script>
I replaced your date separator to use dashes and correct that "2001-02-29"
wrong date. But your assertions are wrong.
Upvotes: 1
Reputation: 449
it looks like DateDiff() is available in VB6...
IF DateDiff("m", FirstDate, SecondDate) >= 6 THEN
return ture
ELSE
return false
END IF
something along those lines perhaps
Upvotes: 1