add/minus months function
Moderators: chulett, rschirm, roy
-
- Participant
- Posts: 27
- Joined: Sun Mar 20, 2005 4:23 am
- Location: Brisbane, Australia
add/minus months function
Is there a standard function to add or take away months from a date.
-
- Charter Member
- Posts: 130
- Joined: Mon Sep 06, 2004 3:05 am
- Location: Dubai,UAE
-
- Participant
- Posts: 27
- Joined: Sun Mar 20, 2005 4:23 am
- Location: Brisbane, Australia
Your date is coming in as a string in the format dd/mm/yy. You don't care about the dd portion and can do a substring on the month and year and perform you math on those. Even though it could be done as a single line in a derivation, a small routine would be best:
Code: Select all
Routine Subtract4MonthsfromDate(InDate)
InDay = InDate[1,2]
InMonth = InDate[3,2]
InYear = InDate[7,2]
OutMonth = InMonth - 4
IF OutMonth < 1
THEN
OutMonth = 12 + OutMonth
OutYear = OutYear - 1
END
Ans = InDay:'/':OutMonth:'/':OutYear
<a href=http://www.worldcommunitygrid.org/team/ ... TZ9H4CGVP1 target="WCGWin">
</a>
</a>
-
- Premium Member
- Posts: 252
- Joined: Mon Sep 19, 2005 10:28 pm
- Location: Melbourne, Australia
- Contact:
Don't know how to do it with iconv/oconv alone, but you could write your own routine without too much difficulty.
- Extract 3 numeric vars from your date: YYYY, MM, and DD. You can use iconv/oconv for this - search for "d code" in the online help.
- Add n/12 to YYYY
- Add mod(n, 12) to MM
- If MM>12, YYYY+=1 and MM-=12
- Reassemble date as YYYY-MM-01 and convert to internal date format
- Add DD days.
- If the final date is > MONTH.LAST(YYYY-MM), then set it to MONTH.LAST(YYYY-MM).
- Extract 3 numeric vars from your date: YYYY, MM, and DD. You can use iconv/oconv for this - search for "d code" in the online help.
- Add n/12 to YYYY
- Add mod(n, 12) to MM
- If MM>12, YYYY+=1 and MM-=12
- Reassemble date as YYYY-MM-01 and convert to internal date format
- Add DD days.
- If the final date is > MONTH.LAST(YYYY-MM), then set it to MONTH.LAST(YYYY-MM).
Ross Leishman
Hi mick
It's always a good practice to check whether the returned value is valid date or not. Have a business logic defined for cases like this either you take the last day of the month or first day of next month etc.
Thanks
You cannot simply substract 4 months from the given date always. Because value "30/06/05 " will give you 30/02/05 which is not a valid date. You have to consider day part as well for the above value.How would you take 4 months from the date 20/01/06 (ie: return a date of 20/09/05).
It's always a good practice to check whether the returned value is valid date or not. Have a business logic defined for cases like this either you take the last day of the month or first day of next month etc.
Thanks
Regards
Siva
Listening to the Learned
"The most precious wealth is the wealth acquired by the ear Indeed, of all wealth that wealth is the crown." - Thirukural By Thiruvalluvar
Siva
Listening to the Learned
"The most precious wealth is the wealth acquired by the ear Indeed, of all wealth that wealth is the crown." - Thirukural By Thiruvalluvar
-
- Participant
- Posts: 54607
- Joined: Wed Oct 23, 2002 10:52 pm
- Location: Sydney, Australia
- Contact:
-
- Participant
- Posts: 27
- Joined: Sun Mar 20, 2005 4:23 am
- Location: Brisbane, Australia
I would have done that in a premium post My favorite was is to do an ICONV on that output date and let the routine "clean it up" and tell me so with the return status code.tcj wrote:Don't forget to check for leap year :D
<a href=http://www.worldcommunitygrid.org/team/ ... TZ9H4CGVP1 target="WCGWin">
</a>
</a>