Subtract Year
Moderators: chulett, rschirm, roy
Subtract Year
Hi
I am trying to subtract year from a given date format but not able to succeed.The input format is 01/15/2005 and my output should be something like 01/15/2002.Please help me in acheiving this.
Thanks
I am trying to subtract year from a given date format but not able to succeed.The input format is 01/15/2005 and my output should be something like 01/15/2002.Please help me in acheiving this.
Thanks
-
- Participant
- Posts: 3337
- Joined: Mon Jan 17, 2005 4:49 am
- Location: United Kingdom
Hi,
You can use following routine where
Arg1 is the input date (01/15/2005)
Arg2 is another date (01/11/1999)
Arg3 is number of years you want to substract from input date(Arg1)
----------
Ans1 = Arg1[7,4] - Arg3
Ans2 = Arg1[1,6]:Ans1
Ans3 = Iconv(Ans2,"D2")
Ans4 = Oconv(Ans3,"D")
Ans5 = Oconv(Arg2,"D")
IF Ans3 > Iconv(Arg2,"D2") then
Ans = Ans4 : ' GT ' : Ans5
end
else
Ans = Ans4 : ' LT ' : Ans5
end
----
Ketfos
You can use following routine where
Arg1 is the input date (01/15/2005)
Arg2 is another date (01/11/1999)
Arg3 is number of years you want to substract from input date(Arg1)
----------
Ans1 = Arg1[7,4] - Arg3
Ans2 = Arg1[1,6]:Ans1
Ans3 = Iconv(Ans2,"D2")
Ans4 = Oconv(Ans3,"D")
Ans5 = Oconv(Arg2,"D")
IF Ans3 > Iconv(Arg2,"D2") then
Ans = Ans4 : ' GT ' : Ans5
end
else
Ans = Ans4 : ' LT ' : Ans5
end
----
Ketfos
-
- Charter Member
- Posts: 199
- Joined: Tue Jan 18, 2005 2:50 am
- Location: India
Hi,
You had mentioned that routine is not correct.
If will not matter what date mask you use in Oconv function which is
just the display format.
You can output the date in diferent formats in Oconv, still it is going to to return correct results, irrespective of the date format.
As long as input format is like 01/15/2005, the results will be ok.
Thanks
Ketfos
You had mentioned that routine is not correct.
If will not matter what date mask you use in Oconv function which is
just the display format.
You can output the date in diferent formats in Oconv, still it is going to to return correct results, irrespective of the date format.
As long as input format is like 01/15/2005, the results will be ok.
Thanks
Ketfos
You can simply work with the iconv and oconv-functions.
You american may ignore it, but there are a lot of date-formats around the world even for the christian (gregorianian) Dateformat. You first have to check which one you have as source.
if you have for sure "two for month"-"slash"-"two day"-"slash"-"four for the year" you always have with slashes 10 characters. (If you have something else or something variable as dateformat you first have to bring it via iconv and oconv in a fix format (always the same number of charactes).
Then you divide into the rest an the year, for the above example it is:
*** Transfer Argument to variable
MyDate=Arg1
YearsToAdd=Arg2
*** make year-Part = last 4 characters
MyYear=MyDate[4]
*** make moth-day-Part = first 6 characters
MyMonthDay=MyDate[1,6]
*** substract (-) or add (+) years
NewYear=MyYear+YearsToAdd
*** Unite Yearpart and MothDayPart
Ans=MyMonthDay:NewYear
So simply copy the brown text in a routine an choose two arguments. Thats it. As mentioned if you have other dateformats or variable Date-formats you have to change it a bit.
Have an nice day
Wolfgang
You american may ignore it, but there are a lot of date-formats around the world even for the christian (gregorianian) Dateformat. You first have to check which one you have as source.
if you have for sure "two for month"-"slash"-"two day"-"slash"-"four for the year" you always have with slashes 10 characters. (If you have something else or something variable as dateformat you first have to bring it via iconv and oconv in a fix format (always the same number of charactes).
Then you divide into the rest an the year, for the above example it is:
*** Transfer Argument to variable
MyDate=Arg1
YearsToAdd=Arg2
*** make year-Part = last 4 characters
MyYear=MyDate[4]
*** make moth-day-Part = first 6 characters
MyMonthDay=MyDate[1,6]
*** substract (-) or add (+) years
NewYear=MyYear+YearsToAdd
*** Unite Yearpart and MothDayPart
Ans=MyMonthDay:NewYear
So simply copy the brown text in a routine an choose two arguments. Thats it. As mentioned if you have other dateformats or variable Date-formats you have to change it a bit.
Have an nice day
Wolfgang
-
- Participant
- Posts: 54607
- Joined: Wed Oct 23, 2002 10:52 pm
- Location: Sydney, Australia
- Contact:
Don't forget to check that the day and month are valid for the new year. For example, subtracting one year from 2004-02-29 should not return 2003-02-29 because this is not a valid date. What it should return you will need to decide, and then document as a business rule (in the long description of the Routine at the very least).
IBM Software Services Group
Any contribution to this forum is my own opinion and does not necessarily reflect any position that IBM may hold.
Any contribution to this forum is my own opinion and does not necessarily reflect any position that IBM may hold.
-
- Charter Member
- Posts: 199
- Joined: Tue Jan 18, 2005 2:50 am
- Location: India
Just replying to your note, I have mentioned the format given for ICONV is incorrect NOT OCONV.ketfos wrote:Hi,
You had mentioned that routine is not correct.
If will not matter what date mask you use in Oconv function which is
just the display format.
You can output the date in diferent formats in Oconv, still it is going to to return correct results, irrespective of the date format.
As long as input format is like 01/15/2005, the results will be ok.
Thanks
Ketfos
if the format given in ICONV is not same as your input date value, then proper conversion wont happen, and then whatever format you use in OCONV wont give you desired result.
Shantanu Choudhary
-
- Participant
- Posts: 3337
- Joined: Mon Jan 17, 2005 4:49 am
- Location: United Kingdom
I agree with Ray on the Feb date matter.
You can perform OConv(IConv(dt)) to obtain the correct date in that case.
But the requirement from the initial user is not yet clear as he has stated that he wants to find 'difference' between two dates.
You can perform OConv(IConv(dt)) to obtain the correct date in that case.
But the requirement from the initial user is not yet clear as he has stated that he wants to find 'difference' between two dates.
I have to compare the output with another date of same format but for some reasons it is not giving correct results.Please advice.