javascript - Want to use datepicker to be able to calculate weekdays and subtract weekends -
javascript - Want to use datepicker to be able to calculate weekdays and subtract weekends -
i have simple holiday time app building in rails. have javascript calculates business days , subtracts weekends datepicker. seems it's not working subtract days , input amount in range_days
text box. help appreciated!
this app.js
function calcbusinessdays(ddate1, ddate2) { // input given date objects var iweeks, idatediff, iadjust = 0; if (ddate2 < ddate1) homecoming -1; // error code if dates transposed var iweekday1 = ddate1.getday(); // day of week var iweekday2 = ddate2.getday(); iweekday1 = (iweekday1 == 0) ? 7 : iweekday1; // alter sunday 0 7 iweekday2 = (iweekday2 == 0) ? 7 : iweekday2; if ((iweekday1 > 5) && (iweekday2 > 5)) iadjust = 1; // adjustment if both days on weekend iweekday1 = (iweekday1 > 5) ? 5 : iweekday1; // count weekdays iweekday2 = (iweekday2 > 5) ? 5 : iweekday2; // calculate differnece in weeks (1000ms * 60sec * 60min * 24hrs * 7 days = 604800000) iweeks = math.floor((ddate2.gettime() - ddate1.gettime()) / 604800000) if (iweekday1 <= iweekday2) { idatediff = (iweeks * 5) + (iweekday2 - iweekday1) } else { idatediff = ((iweeks + 1) * 5) - (iweekday1 - iweekday2) } idatediff -= iadjust // take business relationship both days on weekend homecoming (idatediff + 1); // add together 1 because dates inclusive } $('#leave_start, #leave_end').change(function() { var d1 = $("#leave_start").val(); var d2 = $("#leave_end").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 newstartdate = new date(); newstartdate.setfullyear(startdate1.getyear(), startdate1.getmonth(), startdate1.getday()); var newenddate = new date(); newenddate.setfullyear(enddate1.getyear(), enddate1.getmonth(), enddate1.getday()); var days = calcbusinessdays(newstartdate, newenddate); if (days > 0) { $('#range_days').val(days); } else { $('#range_days').val(0); } });
this entry.coffee.js datepicker is..
jquery -> $('#leave_start').datepicker({ mindate: 0 }); $('#leave_end').datepicker({ mindate: 0 });
this view...
%table.table.table-bordered.table-striped{:style => 'table-layout:fixed; width:100% !important;'} %th.lt leave start: %td.lt= f.text_field :leave_start, :label => false, :id => 'leave_start', :input_html => {:value => ''} %td.lt= f.error :leave_start, :class => 'er' %table.table.table-bordered.table-striped{:style => 'table-layout:fixed; width:100% !important;'} %th.lt leave end: %td.lt= f.text_field :leave_end, :label => false, :id => 'leave_end', :input_html => {:value => ''} %td.lt= f.error :leave_end, :class => 'er' %table.table.table-bordered.table-striped{:style => 'table-layout:fixed; width:100% !important;'} %th.lt amount of days: %td.lt= f.text_field :range_days, :label => false, :id => 'range_days', :input_html => {:value => ''} %td.lt= f.error :range_days, :class => 'er' %table.table.table-bordered.table-striped{:style => 'table-layout:fixed; width:100% !important;'} = f.button :submit, "submit", :class => 'btn btn-primary', :style => 'margin-left:50px;'
javascript jquery ruby-on-rails ruby
Comments
Post a Comment