Normal Topic adding number of months to a date (Read 462 times)
CapitalG
Full Member
Members
***
Offline



Posts: 143
Location: Phoenix, Arizona
Joined: Mar 4th, 2003
adding number of months to a date
Oct 23rd, 2007 at 3:03pm
Print Post Print Post  
Using 2.X

I am stuck with a calculation.   I have a Date field(StartDate) and a number field(PMonths).  I simply need to calculate a new date that will be PMonths in the future from the StartDate.  

Example:  
StartDate = 12/10/2007
PMonths = 5

Calculated date should be 05/10/2008

Thanks for any pointers.
  
Back to top
 
IP Logged
 
Hammer
YaBB Administrator
Lanticans
*****
Offline


Fire bad. Tree pretty.

Posts: 3436
Location: Ohio
Joined: Nov 22nd, 2002
Re: adding number of months to a date
Reply #1 - Oct 23rd, 2007 at 3:15pm
Print Post Print Post  
Use this function. It takes the start date and number of months to add. It returns the resulting date.

Code
Select All
FUNCTION AddMonths(vDate as Date, vAdd as Int) as Date
var vRet as Date
var vYear as Int
var vMonth as Int

	vRet = vDate
	vYear = @Int(@Year(vDate))
	vMonth = @Month(vDate)
	vMonth = vMonth + vAdd
	While vMonth > 12
	{
		vYear = vYear + 1
		vMonth = vMonth - 12
	}
	vRet = @Str(vYear) + "/" + @Text(2 - @Len(@Str(vMonth)), "0") + @Str(vMonth) + @Right(vDate, 3)
	Return vRet

END FUNCTION 



Usage Example:
Code
Select All
var vLoop as Int

	For vLoop = 1 to 30
		WriteLn(AddMonths(@Date, vLoop))
	Next 


  

- Hammer
The plural of anecdote is not data.
Back to top
IP Logged
 
CapitalG
Full Member
Members
***
Offline



Posts: 143
Location: Phoenix, Arizona
Joined: Mar 4th, 2003
Re: adding number of months to a date
Reply #2 - Oct 23rd, 2007 at 3:17pm
Print Post Print Post  
Thanks  Grin
  
Back to top
 
IP Logged