Sunday, July 6, 2014

Find Date Time Difference through X++ code

Date Time Difference :


To find the time difference in the given two date's. Using the below class we can find the total time difference and also the time difference in  the month and year wise difference. 

Class : TestDateTimeDifference 


Class Declaration : 


class TestDateTimeDifference
{
    int n,x,y,z,frommonth,tomonth,diffmonth,fromyear,toyear,remains;
    date d;
    System.DateTime temp,tempTo,tempFrom;
    System.TimeSpan totalDiff,fromDiff,toDiff,val;
}  

Method :  TimeDifferenceFind 


Public void timeDifferenceFind(utcDateTime _startDate,utcDateTime _endDate)
{
    tempFrom = _startDate;
    tempTo = _endDate;
    TotalDiff = tempTo.Subtract(tempFrom);
    info("Total Difference :  ");
    info(TotalDiff.ToString());

    fromyear = DateTimeUtil::year(_startDate);
    toyear   = DateTimeUtil::year(_endDate);
    frommonth = DateTimeUtil::month(_startDate);
    tomonth   = DateTimeUtil::month(_endDate);

    z= (toyear - fromyear);
    for(y=0;y<=z;y++)
    {
        if(z==0)        //To Find the Time Difference in same Year
            x = 0;
       
        if(z==1 && y==0)        //To find the Time Differnce for two differnt continuous years
             x=1;       
        else if(z==1 && y==1)
             x=3;
       
        if(z>1 && y==0)         // To find the Difference in two different years
             x = 1;
        else if(z>1 && y<z)
             x = 2;
        else if(z>1 && y==z)
             x = 3;
       
        switch(x)
        {
            case 0 :

                    info(strFmt("In %1 : ",fromyear));
                    this.startMonthDiff(_startDate);
                    n = frommonth + 1;
                    remains = tomonth - 1;
                    this.otherMonthsDiff(remains,n,fromyear);
                    this.endMonthDiff(_endDate);
                    break;

            case 1 :
                    frommonth = DateTimeUtil::month(_startDate);
                    tomonth   = 12;
                    info(strFmt("In %1 : ",fromyear));
                    this.startMonthDiff(_startDate);
                    n = frommonth + 1;
                    remains = tomonth;
                    this.otherMonthsDiff(remains,n,fromyear);
                    break;

            case 2:
                    frommonth = 0;
                    tomonth   = 12;
                    fromyear = fromyear+1;
                    info(strFmt("In %1 : ",fromyear));
                    n = frommonth + 1;
                    remains = tomonth;
                    this.otherMonthsDiff(remains,n,fromyear);
                    break;
            case 3 :
                    frommonth = 1;
                    tomonth   = DateTimeUtil::month(_endDate);
                    info(strFmt("In %1 : ",toyear));
                    n = frommonth;
                    remains = tomonth-1;
                    this.otherMonthsDiff(remains,n,toyear);
                    this.endMonthDiff(_endDate);
                    break;
        }
    }
} 


Method : OtherMonthsDiff 


Public void otherMonthsDiff(int _remain,int _start,int _dateYear)
{
    while(_start<=_remain)
    {
        d = mkDate(1,_start,_dateYear);
        d = endmth(d);
        val = new System.TimeSpan(dayOfMth(d),0,0,0,0);
        info(strFmt("The Time difference in  %1 is : ",mthName(_start)));
        info(val.ToString());
        _start++;
    }
} 


Method : StartMonthDiff 


Public System.TimeSpan startMonthDiff(utcDateTime _datefrom)
{
    d = endmth(DateTimeUtil::date(_datefrom));
    temp = mkDate(dayOfMth(d),frommonth,fromyear);
    fromdiff = temp.Subtract(_datefrom);
    info(strFmt("The Time difference in  %1 is : ",mthName(frommonth)));
    info(fromdiff.ToString());
    return fromDiff;
} 


Method : EndMonthDiff 


Public System.TimeSpan endMonthDiff(utcDateTime _dateTo)
{
    d = dateStartMth(DateTimeUtil::date(_dateTo));
    temp = mkDate(dayOfMth(d),tomonth,toyear);
    toDiff = tempto.Subtract(temp);
    toDiff = toDiff.Add(new System.TimeSpan(1,0,0,0,0));
    info(strFmt("The Time difference in   %1 is : ",mthName(tomonth)));
    info(toDiff.ToString());
    return toDiff;
} 


Job :


 DateTimeDifference  dateTimeDifference;   
    dateTimeDifference = new DateTimeDifference();

    dateTimeDifference.timeDifferenceFind(new System.DateTime(2012, 1, 15,23,15,0),new System.DateTime(2014, 2, 10,10,15,0));

Output :