A clarion version of System.AddMonths
Posted on Friday, November 20th, 2009
Today at work I needed some code to Add “x” months to a given date. System.AddMonths looked like the kind of thing I was after but I needed it implemented in clarion code.
Not hard but nice to have.
Thanks Mark for the assistance!
Some examples:
- 05-Oct-2009 + 1 Month = 05-Nov-2009
- 31-Oct-2990 + 1 Month = 30-Nov-2009
AddMonths PROCEDURE (LONG pDate, BYTE pMonths) !,LONG originalDay BYTE returnDate LONG CODE originalDay = Day(pDate) returnDate = Date(Month(pDate) + pMonths, Day(pDate), Year(pDate)) IF Day(returnDate) <> originalDay returnDate = Date(Month(returnDate), 1, Year(returnDate)) - 1 END RETURN returnDate
Categorized as Clarion, Clarion, Examples
One problem I found using this type of function, was that, given your #2 example.. then adding a month at a time your series would look like..
31-Oct-2009
30-Nov-2009
30-Dec-2009 ! Here I would want 31-Dec
30-Jan-2010 ! Here I would want 31-Jan
28-Feb-2010 ! OK
28-Mar-2009 ! Here I would want 31-Mar
I wrote a func to manage it my way, and it is on Clarionmag I think.
email to globaljohnREMOVEatREMOVE gmail.com if anyone wants a copy.
John Griffiths
Hi John, thanks for the comment. Yes I would love a copy of your function please!
I see what you mean by adding months in series but on the other hand, the function is doing what it claims to do
You could do something like this in clarion but of course this is only if you specifically want the last day of the month:
thisDate = '31-10-2009'
LOOP i = 1 TO 12
thisDate = GetLastDayOfMonth(AddMonths(thisDate, i))
END
Just for completeness I did a test on the .Net version to see what happens. It does the same thing as you described.
(Using powershell which is really neat for this kind of adhoc testing!):
PS C:> $A = Get-Date 31/10/2006; for ($i = 0; $i -lt 11; $i++) {$A = $A.AddMonths($i); $A}
Tuesday, 31 October 2006 12:00:00 AM
Thursday, 30 November 2006 12:00:00 AM
Tuesday, 30 January 2007 12:00:00 AM
Monday, 30 April 2007 12:00:00 AM
Thursday, 30 August 2007 12:00:00 AM
Wednesday, 30 January 2008 12:00:00 AM
Wednesday, 30 July 2008 12:00:00 AM
Saturday, 28 February 2009 12:00:00 AM
Wednesday, 28 October 2009 12:00:00 AM
Wednesday, 28 July 2010 12:00:00 AM
Saturday, 28 May 2011 12:00:00 AM
Hi Brahn, here is source for my function. Enjoy.
! The Prototype
NMDate FUNCTION(LONG piFromDate,LONG piAnnivOf),LONG
! The Function CODE
NMDate PROCEDURE (LONG piFromDate,LONG piAnnivOf)!,LONG ! Declare Procedure
! piFromDate is the date in the month which we want to
! advance and get the Next Months date. The piAnnivOf is
! the day of month value we wish to use as the anniversary
NMDater LONG,AUTO
DAYGOT LONG,AUTO
CODE
CASE piAnnivOf
OF 1 to 28
NMDater = date(MONTH(piFromDate)+1, piAnnivOf ,year(piFromDate))
OF 29 to 31
NMDater= date(MONTH(piFromDate)+1, piAnnivOf ,year(piFromDate))
DAYGOT = DAY(NMDater)
IF DAYGOT < 5
NMDater = NMDater – DAYGOT
END
ELSE
NMDater = 0
END ! CASE
RETURN NMDater
John Griffiths