2012-03-16

Function monthDiff

Mencari rentang/jumlah bulan dari suatu periode. Masih mencari function terbaik.
int monthDiff(TransDate _fromDate, TransDate _toDate)
{
    TransDate   fromDate = _fromDate, toDate = _toDate;
    int         yrDiff;
    int         totalMonth;
    int         fdtDay = dayofmth(fromDate);
    //int         tdtDay = dayofmth(toDate);
    int         frMth = mthofyr(fromDate);
    int         toMth = mthofyr(toDate);
    int         countMth;
    int         fYear = year(fromDate), tYear = year(toDate);
    date        procDt;
    ;
    yrDiff = yearDiff(toDate, fromDate);

    countMth = frMth;
    totalMonth = 0;

    if(!yrDiff)
    {
        while(fYear<=tYear)
        {
            while(countMth<=12)
            {
                if((fYear == tYear) && (countMth == toMth))
                    break;

                countMth++;
                totalMonth++;
            }
            fYear++;
            countMth = 1;
        }
    }
    else
    {
        tYear = tYear - yrDiff;

        while(fYear<=tYear)
        {
            while(countMth<=12)
            {
                if((fYear == tYear) && (countMth == toMth))
                    break;

                countMth++;
                totalMonth++;
            }
            fYear++;
            countMth = 1;
        }
        totalMonth += (12 * yrDiff);
    }
    procDt =  mkdate(fdtDay,toMth,fYear-1);
    if(!(dayofmth(procDt)<fdtDay))
        procDt--;

    if(toDate<(procDt))
        totalMonth--;
    
    fYear = year(fromDate);
    if(fYear == tYear && frMth == toMth && fromDate == dateStartMth(fromDate) && toDate == dateEndMth(toDate))
        totalMonth++;

     return totalMonth;
}