简体   繁体   中英

calculate number of days between two dates and dates are selecting by pickadate in jquery

i have 3 text fields in rails view

<div>
<%= f.label :from_date %>
<%= f.text_field :start_date, :class => 'datepicker' %>
</div>
<div>
<%= f.label :to_date %>
<%= f.text_field :end_date, :class => 'datepicker' %>
</div>
<div>
<label>Days</label
<%= f.text_field :days,:readonly => true %>
</div>

by selecting dates from pickadate. mt code in application.js is :

$('.datepicker).pickadate();

i want to calculate the days between dates. my calculate method is defined below:-

function calculate() {
 var d1 = $('#startdate').pickadate('getDate');
var d2 = $('#enddate').pickadate('getDate');
var diff = 1;
if (d1 && d2) {
diff = diff + Math.floor((d2.getTime() - d1.getTime()) / 86400000);
}
$('#days').val(diff);
 }

but it does not work properly there is something missing. i didn't get it. please help me. Thanks in advance.

When dealing with this kind of date manipulation I really like http://momentjs.com/ library.

You'd specifically use the from() method: http://momentjs.com/docs/#/displaying/from/

Following code will give you difference between two dates.

Date.daysBetween = function( date1, date2 ) {
  //Get 1 day in milliseconds
  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 = date2_ms - date1_ms;

  // Convert back to days and return
  return Math.round(difference_ms/one_day); 
}

d1 = new Date('2015/5/22');
console.log(d1);
d2 = new Date('2015/6/12');

console.log(Date.daysBetween(d1, d2)); 

example : jsfiddle

Hi am new in ruby on rails and jquery pickadate function is quite new for me. Functions are defined in amsul.ca/pickadate.js/

from read this site. I am just trying to implement my solution.

    $('#start_date').pickadate({
    format: 'dd/mm/yyyy',
    selectMonths: true,
    selectYears: 25
    }
    });
    $('#end_date').pickadate({
    format: 'dd/mm/yyyy',
    selectMonths: true,
    selectYears: 25
    }
  });

call a function to calculate no of days without weekends.

var firstDay = new Date($( '#leave_start_date' ).val() ),
        lastDay = new Date($( '#leave_end_date' ).val() ),
        daysDiff = calcHolidays( firstDay,lastDay );
   if ( isNaN( daysDiff ) )
         $( '#leave_days' ).val( "" );
   else
        $( '#leave_days' ).val( daysDiff );
    }

calcHolidays() function is defined below.

function calcHolidays( dDate1, dDate2 ) {
var weeks, dateDiff, weekDay1, weekDay2;
if ( dDate2 < dDate1 ) return -1;
weekDay1 = dDate1.getDay(),
weekDay2 = dDate2.getDay();
weeks = Math.floor(( dDate2.getTime() - dDate1.getTime() ) / 604800000);
if ( weekDay1 <= weekDay2 )
dateDiff = ( weeks * 5 ) + ( weekDay2 - weekDay1 );
else
dateDiff = (( weeks + 1 ) * 5) - ( weekDay1 - weekDay2 );
return ( dateDiff + 1 );
}

call calculate function in .datepicker change method. all functionality is going pretty well according to requirments. thanx for help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM