Thursday 22 December 2011

Calculate difference between two dates

Example-1:

Find day difference between two dates (excluding weekend days)

<script type="text/javascript">

    $
("#startdate, #enddate").change(function() {      

   
var d1 = $("#startdate").val();
   
var d2 = $("#enddate").val();

           
var minutes = 1000*60;
           
var hours = minutes*60;
           
var day = hours*24;

           
var startdate1 = getDateFromFormat(d1, "d-m-y");
           
var enddate1 = getDateFromFormat(d2, "d-m-y");

           
var days = 1 + Math.round((enddate1 - startdate1)/day);            

   
if(days>0)
   
{ $("#noofdays").val(days);}
   
else
   
{ $("#noofdays").val(0);}


   
});

   
</script>

Example-2:

Calculate Business Days between two dates




  1. function calcBusinessDays(dDate1, dDate2) { // input given as Date objects
  2.  
  3. var iWeeks, iDateDiff, iAdjust = 0;
  4.  
  5. if (dDate2 < dDate1) return -1; // error code if dates transposed
  6.  
  7. var iWeekday1 = dDate1.getDay(); // day of week
  8. var iWeekday2 = dDate2.getDay();
  9.  
  10. iWeekday1 = (iWeekday1 == 0) ? 7 : iWeekday1; // change Sunday from 0 to 7
  11. iWeekday2 = (iWeekday2 == 0) ? 7 : iWeekday2;
  12.  
  13. if ((iWeekday1 > 5) && (iWeekday2 > 5)) iAdjust = 1; // adjustment if both days on weekend
  14.  
  15. iWeekday1 = (iWeekday1 > 5) ? 5 : iWeekday1; // only count weekdays
  16. iWeekday2 = (iWeekday2 > 5) ? 5 : iWeekday2;
  17.  
  18. // calculate differnece in weeks (1000mS * 60sec * 60min * 24hrs * 7 days = 604800000)
  19. iWeeks = Math.floor((dDate2.getTime() - dDate1.getTime()) / 604800000)
  20.  
  21. if (iWeekday1 <= iWeekday2) {
  22. iDateDiff = (iWeeks * 5) + (iWeekday2 - iWeekday1)
  23. } else {
  24. iDateDiff = ((iWeeks + 1) * 5) - (iWeekday1 - iWeekday2)
  25. }
  26.  
  27. iDateDiff -= iAdjust // take into account both days on weekend
  28.  
  29. return (iDateDiff + 1); // add 1 because dates are inclusive
  30.  
  31. }

Example-3:
Calculating the Number of Days Between Any Two Dates

<script language="JavaScript" type="text/javascript">
<!--

function days_between(date1, date2) {

    // The number of milliseconds in one day
    var ONE_DAY = 1000 * 60 * 60 * 24

    // Convert both dates to milliseconds
    var date1_ms = date1.getTime()
    var date2_ms = date2.getTime()

    // Calculate the difference in milliseconds
    var difference_ms = Math.abs(date1_ms - date2_ms)
    
    // Convert back to days and return
    return Math.round(difference_ms/ONE_DAY)

}

//-->
</script>
This function accepts

Example-4
<script type="text/javascript">
function PreSaveAction()
{
     //var Date1=document.getElementById("ctl00_m_g_a3891a90_455d_467d_888a_d925ce4fc814_ctl00_ctl04_ctl01_ctl00_ctl00_ctl04_ctl00_ctl00_DateTimeField_DateTimeFieldDate");
 
 var dtStDate = new Date(document.getElementById("ctl00_m_g_c19e22b2_bb2e_4ea5_8598_6c207adec1b2_ctl00_ctl04_ctl01_ctl00_ctl00_ctl04_ctl00_ctl00_DateTimeField_DateTimeFieldDate").value);
 //Getting End date value from the control
    var dtEndDate = new Date(document.getElementById("ctl00_m_g_c19e22b2_bb2e_4ea5_8598_6c207adec1b2_ctl00_ctl04_ctl02_ctl00_ctl00_ctl04_ctl00_ctl00_DateTimeField_DateTimeFieldDate").value);
    var one_day=1000*60*60*24; 
   
   if(dtStDate !="NaN" &&  dtEndDate !="NaN")  
 {
 
  //Assigning the Difference in days to the control
        var fnlValue=Math.ceil((dtEndDate.getTime()-dtStDate.getTime())/(one_day))+1;  
        //var fnlValue=((dtEndDate.getTime()-dtStDate.getTime())/(one_day))+1;  
  var today=new Date()
  var selectedDay=new Date(dtStDate.getFullYear(), dtStDate.getMonth(),dtStDate.getDate(1)) //Month is 0-11 in JavaScript
  //Calculate difference btw the two dates, and convert to days
  var leaveDuration =(Math.ceil((selectedDay.getTime()-today.getTime())/(one_day))); 
  //var leaveDuration=(Math.ceil((selectedDay.getTime()-today.getTime())/(one_day)));
  //var leaveDuration=calcBusinessDays(selectedDay,today);
  //alert(leaveDuration); 
  if(dtEndDate < dtStDate )
  {
          alert('Please check your selected dates');
    return false;
  }
  if(leaveDuration < -60)
  {
        alert('Sorry! You have selected dates that have passed more than 60 days');
         return false;
  }
  else
  {
  if(leaveDuration < -0)
    {
      if(confirm("Are you sure you want to proceed? "+"\n \n"+"You have selected dates that have passed. This is only possible if you have been absent on account of illness."))
      {       
        return true;
            }
            else
            {
             return false;
            }
    }
  }
  if(fnlValue >4)
  {
    if(leaveDuration <=14)
    {   
  
   if(confirm("WARNING :Are you sure you want to continue?"+"\n \n"+"Your application for Leave is late. There is insufficient notice as per the norms laid down by the Leave Policy. Your request for leave is being forwarded, but your Manager may not consider it on grounds of Short Notice."))
      {          
         return true;
      }
      else
   {
      
       return false;
   }         
    }
  }
  else
  {
    //alert('Your total leaves are applied :'+ fnlValue);
    return true;
  }
  return true;   
  } 
}
</script>

No comments:

Post a Comment