Diff. between 2 dates in terms of years, months and days

manish_s_inmanish_s_in Member Posts: 4
edited 2007-08-14 in Dynamics AX
Hi everyone,
is there any standard method in axapta, in which I pass two dates as argument(say me date of birth and current date) and it will return me the years, months and days in between them.
For ex. I entered my date of birth and current date, it will return me , how old I am , in terms of years, months and days.

Any idea is helpful...
Thanks

Manish
Nothing is impossible

Comments

  • kashperukkashperuk Member Posts: 53
    Here is a method you can add into your Global class:

    static str myDateDiff(date _d1, date _d2, boolean _include = false)
    {
    date m_d1, m_d2; // m_d2 >= m_d1
    str _s;
    int m_year, m_mth, m_days;
    ;
    if(_d1<_d2)
    {
    m_d1 = _d1;
    m_d2 = _d2;
    } else
    {
    m_d1 = _d2;
    m_d2 = _d1;
    }
    m_year = yearDiff(m_d2,m_d1);
    m_mth = mthOfYr(m_d2) - mthOfYr(m_d1);
    if (m_mth < 0)
    m_mth += 12;
    m_days = dayOfMth(m_d2) - dayOfMth(m_d1);
    if (m_days < 0)
    {
    m_mth--;
    m_days += dayOfMth(dateEndMth(m_d1));
    }
    if (m_mth < 0)
    m_mth += 12;
    if (_include) m_days++;
    if (m_days == dayOfMth(dateEndMth(m_d2)))
    {
    m_mth++;
    m_days = 0;
    }
    _s = strltrim(strrtrim(strfmt("%1 year(s), %2 month(s), %3 day(s)", m_year, m_mth, m_days)));
    return _s;
    }

    and here is an example of how to use it:

    static void tutorial_DateDifference(Args _args)
    {
    date birthday = mkDate(27,01,1984);
    date todayDate = SystemDateGet();
    ;
    print myDateDiff(todayDate ,birthday);

    pause;
    }
    Vanya Kashperuk,
    My blog - http://kashperuk.blogspot.com
    MorphX IT in Russian - http://www.lulu.com/content/723888
    Inside Dynamics AX 4.0 in Russian - http://www.ozon.ru/context/detail/id/3714582
Sign In or Register to comment.