View previous topic :: View next topic
|
Author |
Message |
nareshkareti
New User
Joined: 22 Jul 2008 Posts: 33 Location: Chennai
|
|
|
|
Hi,
I have a requirement where i have to get the previous date, after subtracting a number (which denotes months) from current date.
Eg: If the months number is 24, it means we have to subtract 24 months from the current date (YYYY-MM-DD) and arrive at the old date (YYYY-MMM-DD)
Please let me know if more details are needed |
|
Back to top |
|
|
guptae
Moderator
Joined: 14 Oct 2005 Posts: 1208 Location: Bangalore,India
|
|
|
|
Hello Naresh,
Could you please provide an example for i/p & O/p? |
|
Back to top |
|
|
enrico-sorichetti
Superior Member
Joined: 14 Mar 2007 Posts: 10873 Location: italy
|
|
|
|
the issue has been debated quite a few times...
first of all have Your organization define clearly the <algorithm>
... no issues if 1 <= day <= 28 going back two months will not hurt anybody
somebody must clearly define ( from an accounting and legal point of view )
what should be done when subtracting/adding will yield a non existing day
if the program is a db2 program You can use the db2 date/time facilities
in this case when adding subtracting months non existent days will be mapped to the last day of the month
otherwise You will have to write the logic Yourself ...
not very complicated anyway
split the date in the three components year month day
subtract the number of months from the month...
if the result is negative keep adding add 12 to the month and subtract 1 from the year, until positive
if the day is less or equal to the last day of the month ( computed one ) use it,
otherwise use the last day of the month ( computed one )
here is a rexx snippet that will do it ( just to show the logic )
Code: |
date = date("S")
tabd = "31 28 31 30 31 30 31 31 30 31 30 31"
do diff = 1 to 24
date = "20091231"
parse var date year +4 mnth +2 day
mnth = mnth - diff
do while ( mnth <= 0 )
mnth = mnth + 12
year = year - 1
end
last = word(tabd,mnth)
if mnth = 2 then ,
last = last + ( (year//400=0)|((year//100<>0 )&(year//4=0)) )
if last < day then ,
day = last
say date right(diff,2,"0") year || right(mnth,2,"0") || right(day ,2,"0")
end
exit |
|
|
Back to top |
|
|
nareshkareti
New User
Joined: 22 Jul 2008 Posts: 33 Location: Chennai
|
|
|
|
I need logic or functions in COBOL
The "month quantity" is taken from a DB2 table and has to subtract from the CURRENT date to give a new date (which is always older dates).
The result should be a valid old date in the correct date format (YYYY-MM-DD)
Say if the month quantity is 2 and current date is 2009-11-23, the result should be 2009-09-23.
I can reformat the logic given in REXX to COBOL, but i just wanted to cross check again. |
|
Back to top |
|
|
enrico-sorichetti
Superior Member
Joined: 14 Mar 2007 Posts: 10873 Location: italy
|
|
|
|
look like You did not read/understand my reply...
You implicitly answered one of my questions ...
since the program is already a db2/sql program there will not be any waste/overhead of resources
by using the db2 date computation facilities!
looks like You / Your organization disregarded the fact that monhs have usually each a different number of days...
as I already said ...
the db2 logic in date computations involving addin/subtracting monts is to use the last day of the target month for non existing days
it means that for march 31 going back one month the computed date will be feb 28/29 depending on leapness
have Your organization' s powers formally approve such algorithm/approach |
|
Back to top |
|
|
enrico-sorichetti
Superior Member
Joined: 14 Mar 2007 Posts: 10873 Location: italy
|
|
|
|
follow on...
to explain better my accounting/legal concerns
Quote: |
Say if the month quantity is 2 and current date is 2009-11-23, the result should be 2009-09-23. |
same subtraction as above
what will be he chosen date on 2010-04-29 2010-04-30
does Your organization realize the issues that might arise ?? |
|
Back to top |
|
|
|