﻿Math.DateDiff = function( pdtBaseDate, pdtCompareDate, psLanguage, psReturnType )
{
    try
    {
       if( pdtBaseDate == null || pdtCompareDate == null ) return pdtBaseDate;
       
       var sLanguage = "en-US";
       if( psLanguage != null ) sLanguage = psLanguage;

       var sReturnType = "d"; //default to DAY
       if( psReturnType != null ) sReturnType = psReturnType;

        var BDyear, BDmonth, BDay, BDen, CDyear, CDmonth, CDday, CDen;

       switch( sLanguage )
       {
          case "pt-BR":
             BDday = pdtBaseDate.substr(0,2);
             BDmonth = pdtBaseDate.substr(3,2);
             BDyear = pdtBaseDate.substr(6,4);
             BDen = BDmonth + '/' + BDday + '/' + BDyear;
             
             CDday = pdtCompareDate.substr(0,2);
             CDmonth = pdtCompareDate.substr(3,2);
             CDyear = pdtCompareDate.substr(6,4);
             CDen = CDmonth + '/' + CDday + '/' + CDyear;

             break;
          case "en-US":
             BDmonth = pdtBaseDate.substr(0,2);
             BDday = pdtBaseDate.substr(3,2);
             BDyear = pdtBaseDate.substr(6,4);
             BDen = BDmonth + '/' + BDday + '/' + BDyear;
             
             CDmonth = pdtCompareDate.substr(0,2);
             CDday = pdtCompareDate.substr(3,2);
             CDyear = pdtCompareDate.substr(6,4);
             CDen = CDmonth + '/' + CDday + '/' + CDyear;

             break;   
       }

       var dtBaseDate  = new Date(BDen);
       var dtCompareDate  = new Date(CDen);
       var dtDiff  = new Date();

       dtDiff.setTime( dtBaseDate.getTime() - dtCompareDate.getTime() );
       
       var dDiff = dtDiff.getTime();

       switch( sReturnType )
       {
          case "w":
             dDiff = Math.floor(dDiff / (1000 * 60 * 60 * 24 * 7));

             break;
          case "d":
             dDiff = Math.floor(dDiff / (1000 * 60 * 60 * 24)); 

             break;
          case "h":
             dDiff = Math.floor(dDiff / (1000 * 60 * 60)); 

             break;
          case "m":
             dDiff = Math.floor(dDiff / (1000 * 60)); 

             break;
          case "s":
             dDiff = Math.floor(dDiff / 1000); 

          case "ms":
             dDiff = Math.floor(dDiff); 

             break;
       }

       return dDiff;
    }
    catch(ex)
    {
        debugHelper.Exception("Math.DateDiff " + ex.message, document.URL, document.lastModified, document.referrer);
    }
    
    return null;
}

