Adding months or years to a date

Adding months or years to a date

Post by Craig Armstron » Mon, 10 Aug 1998 04:00:00



I have asked this before, but no-one has been able to give me a good answer
...

Can anyone tell me how to add a month or a year to a date.  In my
application, I have a "filing date", and I need to calculate a deadline date
which may be, for example, 3 months later.  Obviously, adding 91 or 92 days
doesn't always produce the correct result.

Thanks in advance to anyone who can help me with this.

 
 
 

Adding months or years to a date

Post by Steve or Jane Urbac » Mon, 10 Aug 1998 04:00:00



>I have asked this before, but no-one has been able to give me a good answer
>...

>Can anyone tell me how to add a month or a year to a date.  In my
>application, I have a "filing date", and I need to calculate a deadline
date
>which may be, for example, 3 months later.  Obviously, adding 91 or 92 days
>doesn't always produce the correct result.

>Thanks in advance to anyone who can help me with this.

Only year is simple. the rest is NOT Tested, but you get the idea

Proc Futuredate( period,qty)
;Period is: "Y","M","W"
; Qty is just what it says as in 3W
; returns future date
Private id,im,iyyy

If Qty < 0 Then
   Message "Positive Values only, at this time. "
   Sleep
   Return BlankDate()
EndIf
dmy = Format("D8",yourdatehere)   ; handle Y2k :) and all date formats
If Match( dmy ,"../../..",mm,dd,yyyy) Then
    Switch
         Case period = "Y":
                yyyy = StrVal(NumVal(yyyy) + qty)
               Return DateVal(mm+"/"+dd+"/+yyyy
         Case period = "M":
                  im = NumVal(mm)+qty
                   iyyy = 0
                  If im > 12 Then   ;overflow years needed
                       iyyy = Int(im/12)
                       mm = StrVal(Mod(im,12) )
                      yyyy= StrVAl(NumVal(yyyy)+iyyy)
                   Else
                        mm = StrVal(im)
                   EndIf
                  If DateVal(mm+"/"+dd+"/+yyyy) = "Error" Then ;days in
month to high for the NEW year and month
                        If NumVal(mm) = 12 Then            ;December
                           yyyy= StrVal(NumVal(yyyy)+1) ;make it next year
                           mm = "1" ;and Jan month
                        Else
                           mm = StrVal(mm) + 1   ;make it next month
                        EndIf
                        dd = "1"   ;force to first of the  month
                  EndIf
                  Return DateVal(mm+"/"+dd+"/+yyyy)

           Case period = "W":
                 Return yourdate + (qty*7) ;paradox does the work

    EndSwitch
Else
    Message "Bad Date Format supplied"
   Sleep 1000
   Return BlankDate()
EndIf

EndProc

Have fun
Steve Urbach

 
 
 

Adding months or years to a date

Post by Craig Armstron » Mon, 10 Aug 1998 04:00:00


Wow, that's "simple"?  Simple is "Date = Date + 1Mo" or something like that!

Thanks, though - I'll give it a try!


Quote:>Only year is simple. the rest is NOT Tested, but you get the idea

 
 
 

Adding months or years to a date

Post by krom » Tue, 11 Aug 1998 04:00:00



>I have asked this before, but no-one has been able to give me a good answer
>...

>Can anyone tell me how to add a month or a year to a date.  In my
>application, I have a "filing date", and I need to calculate a deadline
date
>which may be, for example, 3 months later.  Obviously, adding 91 or 92 days
>doesn't always produce the correct result.

Adding months isn't difficult, just a little bit of code is involved.

Here is some code I use:

proc AddMonths(dt date, mths smallint) date
var
    yrs SmallInt
endvar
    yrs = mths / 12
    Return
Date(String(dt.month()+mths-12*yrs)+"/"+string(dt.Day())+"/"+String(dt.Year(
)+yrs))
endproc

proc AddYears(dt date, yrs smallint) date
    Return
Date(String(dt.month())+"/"+string(dt.Day())+"/"+String(dt.Year()+yrs))
endproc

The code will also work for subtracting months, just use a negative number.
But, the AddMonths code will fail if the day of the month doesn't exist.

        -- Kirk

 
 
 

Adding months or years to a date

Post by David Ree » Tue, 11 Aug 1998 04:00:00


Quote:> Can anyone tell me how to add a month or a year to a date.  In my
> application, I have a "filing date", and I need to calculate a deadline
date
> which may be, for example, 3 months later.  Obviously, adding 91 or 92
days
> doesn't always produce the correct result.

Try this -

new.date = STRVAL(MOD(MONTH(old.date) + add.months,12)) + "/" +
                 STRVAL(DAY(old.date)) + "/" +
             STRVAL(INT((MONTH(old.date) + add.months) / 12) +
                              YEAR(old.date) + add.years)

David Reed

 
 
 

Adding months or years to a date

Post by David Ree » Tue, 11 Aug 1998 04:00:00


Craig - I apologize for the previous response - I wrote it without testing
it <g>!
It didn't account for the Mod(12,12) = 0, which would make the month of
December appear as 0..  try this new 'modified' version of my previous
post:

new.date =
   STRVAL(ROUND(MOD(MONTH(old.date) + add.months,12.001),0)) + "/" +
   STRVAL(DAY(old.date)) + "/" +
   STRVAL(INT((MONTH(old.date) + add.months) / 12.001) +
                YEAR(old.date) + add.years)

BTW, new.date is a STRING, take the DATEVAL() of it if you need to..

David Reed

 
 
 

Adding months or years to a date

Post by Steve or Jane Urbac » Tue, 11 Aug 1998 04:00:00


My BUG alert!  The first line was mising a formal parameter
Sorry

Proc Futuredate( yourdatehere,period,qty)


>Wow, that's "simple"?  Simple is "Date = Date + 1Mo" or something like

that!

Sure! it had less than 50 lines of code <G>.

All that code handles 3 flavors of  Futures Weeks, Months and Year.It leaves
the Day of Month alone
EXCEPT when it runs into a shorter month. eg 12/30/97 + 2 M
BTW  you had the usage almost correct.   fdate = futuredate(fromdate,"M",2)

Steve

>Thanks, though - I'll give it a try!


>>Only year is simple. the rest is NOT Tested, but you get the idea

 
 
 

Adding months or years to a date

Post by Steve or Jane Urbac » Tue, 11 Aug 1998 04:00:00


<g> and where did add.years come from?
Steve U

>Craig - I apologize for the previous response - I wrote it without testing
>it <g>!
>It didn't account for the Mod(12,12) = 0, which would make the month of
>December appear as 0..  try this new 'modified' version of my previous
>post:

>new.date =
>   STRVAL(ROUND(MOD(MONTH(old.date) + add.months,12.001),0)) + "/" +
>   STRVAL(DAY(old.date)) + "/" +
>   STRVAL(INT((MONTH(old.date) + add.months) / 12.001) +
>                YEAR(old.date) + add.years)

>BTW, new.date is a STRING, take the DATEVAL() of it if you need to..

>David Reed

 
 
 

Adding months or years to a date

Post by David Ree » Tue, 11 Aug 1998 04:00:00


Quote:> and where did add.years come from?

C'mon, Steve, I didn't think I'd have to explain to you <vbg>!
old.date = Date to add to
new.date = result
add.years = Number of years to add to the date..
add.months = Number of months to add to the date..

Unfortunately though, in review of my 2-minute code, it will fail if the
result is,
say, 9/31/98.. or 2/31/99.. So, I spent a few more minutes and procedurized
it,
allowing addition of days, months, or years..
David Reed

PROC NewDate(old.date,add.days,add.months,add.years)
PRIVATE mm,dd,yyyy

old.date = old.date + add.days
mm = STRVAL(ROUND(MOD(MONTH(old.date) + add.months,12.001),0))
yyyy = STRVAL(INT((MONTH(old.date) + add.months) / 12.001) +
                        YEAR(old.date) + add.years)

; make sure the day isn't greater than the last
; possible day for the newly calc'd month..

dd = STRVAL(MIN(DAY(old.date),
                            DAY((DATEVAL(mm + "/01/" + yyyy) + 32) -
                            DAY(DATEVAL(mm + "/01/" + yyyy) + 32))))

RETURN DATEVAL(mm + "/" + dd + "/" + yyyy)

ENDPROC

 
 
 

Adding months or years to a date

Post by Coach informatisering, Twan van Zeelan » Wed, 12 Aug 1998 04:00:00


Try this method I wrote (in dutch) :

method IncMonth(AanvDatum!Dav date, aantal!Siv smallInt) date
var
 Aantaldagen!Siv,
 AanvDag!Siv,
 AanvMaand!Siv,
 AanvJaar!Siv,
 EindMaand!Siv,
 EindJaar!Siv  smallInt
 EindDatum!Dav  date
EndVar

AanvDag!Siv  = day(AanvDatum!Dav)
AanvMaand!Siv  = month(AanvDatum!Dav)
AanvJaar!Siv  = year(AanvDatum!Dav)

Eindmaand!Siv  = AanvMaand!Siv + Aantal!Siv
EindJaar!Siv  = AanvJaar!Siv + int((Eindmaand!Siv - 1) / 12)
Eindmaand!Siv  = eindmaand!siv - 1
Eindmaand!Siv  = eindmaand!siv.MOD(12) + 1

Aantaldagen!Siv = 0
try
 EindDatum!Dav = date(strVal(AanvDag!Siv - Aantaldagen!Siv) + "-" +
           strVal(Eindmaand!Siv) + "-" +
                        strVal(EindJaar!Siv))
onfail
 Aantaldagen!Siv = Aantaldagen!Siv + 1
   reTry
endTry
return(EindDatum!Dav)

endMethod

It works, even for high number days in a month

Twan van Zeeland


> I have asked this before, but no-one has been able to give me a good answer
> ...

> Can anyone tell me how to add a month or a year to a date.  In my
> application, I have a "filing date", and I need to calculate a deadline date
> which may be, for example, 3 months later.  Obviously, adding 91 or 92 days
> doesn't always produce the correct result.

> Thanks in advance to anyone who can help me with this.