cancel
Showing results for 
Search instead for 
Did you mean: 

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

Former Member
0 Kudos

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

adate = 10/22/2006

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

Please help

Thanks

Accepted Solutions (0)

Answers (12)

Answers (12)

Former Member
0 Kudos

Post Author: gigimonu

CA Forum: Formula

the data doesn't change. I have {PAEMPLOYEE.BEN_DATE_1} displayed in the page header showing the correct date. If I put this formula in Report header it shows 1 years 5 months and 1 days (this is correct because the ben_date_1 is 05/30/2006) but if I drag and drop it in page header it shows 2 years 0 months and 1 days. Just to let you know I have the Details section supress no drill around

Thanks

Former Member
0 Kudos

Post Author: yangster

CA Forum: Formula

the shared totals will have no issues with your dateif it works in the report header then i'm assuming the date changes somehow in the report?if you put the field into the report in different sections do you the date changing in the different sections?

Former Member
0 Kudos

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

Former Member
0 Kudos

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.

Former Member
0 Kudos

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

Former Member
0 Kudos

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

Former Member
0 Kudos

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

Former Member
0 Kudos

Post Author: gigimonu

CA Forum: Formula

it's datetime field

Thanks

Former Member
0 Kudos

Post Author: yangster

CA Forum: Formula

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

Former Member
0 Kudos

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

Thanks for all your help

Former Member
0 Kudos

Post Author: V361

CA Forum: Formula

[Years, Months, Days]

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;

TEMP := DATE(DATEADD("M",YEARS * 12,FROMDATE));)

ELSE

(YEARS := 0;

TEMP := FROMDATE);

//

WHILE TRUE DO

(TEMP := DATE(DATEADD('M',1,TEMP));

IF TEMP > TODATE THEN

EXIT WHILE;

MONTHS := MONTHS + 1);

//

DAYS := DATEDIFF('D',DATE(DATEADD('M',-1,TEMP)),TODATE);

//

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");

//

Former Member
0 Kudos

Post Author: SKodidine

CA Forum: Formula

Check out this thread.

http://technicalsupport.businessobjects.com/cs/forums/thread/6084.aspx