Page 1 of 1

Subtract Year

Posted: Mon Jun 27, 2005 10:11 am
by shiva459
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

Posted: Mon Jun 27, 2005 10:23 am
by Sainath.Srinivasan
Simplest way may be to split the year separately and concat it back after the computation.

Posted: Mon Jun 27, 2005 10:31 am
by shiva459
Hi Sainath

I tried doing that but my comparison logic is not working properly.

I have to compare the output with another date of same format but for some reasons it is not giving correct results.Please advice.

Posted: Mon Jun 27, 2005 12:34 pm
by ketfos
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

Posted: Mon Jun 27, 2005 12:57 pm
by talk2shaanc
ketfos wrote: Ans3 = Iconv(Ans2,"D2")
Ketfos
above is incorrect, the format D2 is for a date value say; 25 MAY 05

Correct format would be:
Ans3= Iconv(Ans2, "D/MDY[2,2,4]")

Posted: Mon Jun 27, 2005 1:09 pm
by ketfos
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

Posted: Mon Jun 27, 2005 3:11 pm
by WoMaWil
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

Posted: Mon Jun 27, 2005 7:15 pm
by ray.wurlod
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).

Posted: Tue Jun 28, 2005 12:38 am
by talk2shaanc
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
Just replying to your note, I have mentioned the format given for ICONV is incorrect NOT OCONV.
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.

Posted: Tue Jun 28, 2005 1:34 am
by elavenil
You can achieve this by OConv(IConv(Arg1,"D/E")-(n*365)-1,"D/E") , where n is the no of years to subtract and the format must be the same as you mentioned in your post.

HTWH.

Regards
Saravanan

Posted: Tue Jun 28, 2005 4:44 am
by Sainath.Srinivasan
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.
I have to compare the output with another date of same format but for some reasons it is not giving correct results.Please advice.