add days + 1 extra if past 29 Feb

DavidWRouse

Registered User.
Local time
Today, 18:16
Joined
Jul 24, 2003
Messages
42
I want to add a varying number of days to a date. using DateAdd.
But when it crosses Feb 29 on a leap year I want to Add Another Day.

Date = 2/feb/00 + 40 days = 13 Mar 00

If it passed Feb 29 Then add extra Day. Making it 14 Mar 00

Not sure how to best test if my date has passed Feb 29.

Has any on any idears. Have done a search bu didnt quiote find what i needed.
 
Hi


This should work

Dim DT As Date
Dim NEWDT As Date

On Error GoTo NOLEAP

DT = CDate("29/02/" & Year(Date))


If DateAdd("D", 40, Date) >= DT Then

NEWDT = DateAdd("D", 41, Date)

Else

NEWDT = DateAdd("D", 40, Date)

End If


NOLEAP:

NEWDT = DateAdd("D", 40, Date)



Bascially cdate will try and force the string "29/02/" &YEAR(DATE)

into 29/02/2003 as this date doesn't exist an error is created which forces the code to NOLEAP: and simply adds 40 days.

If the year is a leap year then the line

If DateAdd("D", 40, Date) >= DT Then

tests to see if adding 40 days will take you past 29/02 (DT) and adds and extra day

NEWDT = DateAdd("D", 41, Date)


if it evaluates to false i.e 02/01/2000 + 40 days is not passed 29/02/2000 then it only adds 40 days

Else

NEWDT = DateAdd("D", 40, Date)

End If



Hope that helps

Chris
 
Code:
NewDate = DateAdd("d", IIf(IsDate(DateSerial(Year(Date()), 2, 29)), 41, 40), Date())
 

Users who are viewing this thread

Back
Top Bottom