Skip to Content
author's profile photo Former Member
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

adate = 10/22/2006

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

Please help

Thanks

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

12 Answers

  • author's profile photo Former Member
    Former Member
    Posted on Oct 23, 2007 at 11:14 AM

    Post Author: SKodidine

    CA Forum: Formula

    Check out this thread.

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

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Oct 24, 2007 at 07:29 PM

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

    //

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    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

    Thanks for all your help

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    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?

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Oct 29, 2007 at 10:22 PM

    Post Author: gigimonu

    CA Forum: Formula

    it's datetime field

    Thanks

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    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

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    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

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    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

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    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.

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    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

    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.