tags:

views:

234

answers:

4

This is not an homework question :).

I would like to code a function that return True if 6 months has 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.

thanks Michele

+1  A: 

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.

Rubens Farias
"2001-02-29 is a correct date every 4 years at least.
David Glass
No, but `2000-02-29` is a valid date.
Rubens Farias
I've corrected my assertion thanks.
systempuntoout
Thanks for your answer but your algorithm don't pass all the assertions with greater and greater or equal.Passing "2000-08-31", "2001-02-28" the function has to return False to pass the assertion!!
systempuntoout
A: 

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

Lazy Bob
However, getting the difference in months doesn't take the days into account, so there's a one month difference between 2010-01-31 and 2010-02-01.
Guffa
+1  A: 

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#.

Guffa
DateAdd is not working. Try this: DateAdd("m", 6,"2000-05-31").. you will get "2000-11-30" and it does not pass the test. On 2000-11-30, 5 months and 30 days are passed but not 6 months as requested.Using DateAdd adding 6 months has to return False in the last three tests.
systempuntoout
@systempuntoout: I see, you added another test... Add -6 monhts from 2000-11-30 and you get 2000-05-30, which is less than 2000-05-31. So, you would have to test both the addition and subtraction variation to catch the corner cases.
Guffa
A: 

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
systempuntoout