Former Member

# convert two dates difference to number of years, months and days

Post Author: gigimonu

CA Forum: Formula

I wanted to write a formula (if there is a function I can use) that can convert a date difference to total number of years, months and days example

?xdate = current date - adate (answer should be 1 years, 0 months and 0 days)

Thanks

10|10000 characters needed characters exceeded

### Related questions

• Former Member
Posted on Oct 23, 2007 at 11:14 AM

Post Author: SKodidine

CA Forum: Formula

10|10000 characters needed characters exceeded
• Former Member
Posted on Oct 24, 2007 at 07:29 PM

Post Author: V361

CA Forum: Formula

&#91;Years, Months, Days&#93;

DATEVAR FROMDATE := DATE(2000,01,01); // FROM DATE

DATEVAR TODATE := CURRENTDATE; // TO DATE

//

NUMBERVAR YEARS;

NUMBERVAR MONTHS;

NUMBERVAR DAYS;

STRINGVAR DIFF;

DATEVAR TEMP;

//

IF TODATE < FROMDATE THEN

(TEMP := TODATE;

TODATE := FROMDATE;

FROMDATE := TEMP);

//

YEARS := DATEDIFF('YYYY',FROMDATE,TODATE);

//

IF YEARS > 2 THEN

(YEARS := YEARS - 2;

ELSE

(YEARS := 0;

TEMP := FROMDATE);

//

WHILE TRUE DO

IF TEMP > TODATE THEN

EXIT WHILE;

MONTHS := MONTHS + 1);

//

//

IF MONTHS > 12 THEN

(YEARS := YEARS + INT(MONTHS/12);

MONTHS := MONTHS MOD 12);

//

DIFF := IIF(YEARS>0 ,TRIM(TOTEXT(YEARS,0)) & " YEARS " ,"0 YEARS ") &

IIF(MONTHS>0,TRIM(TOTEXT(MONTHS,0))& " MONTHS ","0 MONTHS ")&

IIF(DAYS>0 ,TRIM(TOTEXT(DAYS,0)) & " DAYS" ,"0 DAYS");

//

10|10000 characters needed characters exceeded
• Former Member
Posted on Oct 29, 2007 at 09:57 PM

Post Author: gigimonu

CA Forum: Formula

Thanks for the code. It works well if I hardcode the FROM DATE value to FROMDATE

DATEVAR FROMDATE := DATE(2000,01,01); // FROM DATE

But if I assign a field to FROMDATE it fails (it shows wrong years, months and days). see below

DATEVAR FROMDATE := date({PAEMPLOYEE.BEN_DATE_1}); (this fails)//DATEVAR FROMDATE := DATE(2006,05,30); // FROM DATE (this works)

Can you please help me on this. I need to assign a fields name to FROMDATE variable

10|10000 characters needed characters exceeded
• Former Member
Posted on Oct 29, 2007 at 10:17 PM

Post Author: yangster

CA Forum: Formula

is your ben_date_1 a date, datetime, string, or number?

10|10000 characters needed characters exceeded
• Former Member
Posted on Oct 29, 2007 at 10:22 PM

Post Author: gigimonu

CA Forum: Formula

it's datetime field

Thanks

10|10000 characters needed characters exceeded
• Former Member
Posted on Oct 29, 2007 at 10:36 PM

Post Author: yangster

CA Forum: Formula

weird there should be no issues with the formula thengive this a trydateserial(year(field), month(field), day(field)) instead of the date(field) formula you currently have

10|10000 characters needed characters exceeded
• Former Member
Posted on Oct 29, 2007 at 10:51 PM

Post Author: gigimonu

CA Forum: Formula

before I try this, I just want to let you know that If I place this formula in Report Hearder section it works but if I try to put any where like page header, footer it fails. I have more than 4 subreports in my main report. Do you have any idea?

Thanks

10|10000 characters needed characters exceeded
• Former Member
Posted on Oct 29, 2007 at 10:54 PM

Post Author: yangster

CA Forum: Formula

what do the subreports have to do with your formula?are you getting shared variables from them?and formulas should never be evaluated at the page header or footer level as they are reset at every change of page

10|10000 characters needed characters exceeded
• Former Member
Posted on Oct 29, 2007 at 10:55 PM

Post Author: gigimonu

CA Forum: Formula

I tried your new suggestion but it's the same. Is it some thing to do with the placement of the formula in the report? If I place it in the Report Header it works

Thanks.

10|10000 characters needed characters exceeded
• Former Member
Posted on Oct 29, 2007 at 10:56 PM

Post Author: gigimonu

CA Forum: Formula

Yes! I have shared variables declared in the sub-reports to get the sub-report totals to the main report. Does this have any problem