javascript - Datepicker - Calculate Days Excluding Holidays and Weekends -
i've tried several things work, because of lack of understanding of javascript, no success.
i want little script work how does, want calculated days have no weekends or holidays included in sum. help?
javascript:
$(function() { $('#from').datepicker({ showonfocus: false, showtrigger: '#calimg', beforeshowday: $.datepicker.noweekends, pickerclass: 'noprevnext', numberofmonths: 1, dateformat: 'mm-dd-yy', mindate: '0', maxdate: '+1y', onselect: function(datestr) { var min = $(this).datepicker('getdate'); $('#to').datepicker('option', 'mindate', min || '0'); datepicked(); }}); $('#to').datepicker({ showonfocus: false, showtrigger: '#calimg', beforeshowday: $.datepicker.noweekends, pickerclass: 'noprevnext', numberofmonths: 1, dateformat: 'mm-dd-yy', mindate: '0', maxdate: '+1y', onselect: function(datestr) { var max = $(this).datepicker('getdate'); $('#from').datepicker('option', 'maxdate', max || '+1y'); datepicked(); }}); }); var datepicked = function() { var = $('#from'); var = $('#to'); var nights = $('#nights'); var fromdate = from.datepicker('getdate') var todate = to.datepicker('getdate') if (todate && fromdate) { var difference = 0; var oneday = 1000*60*60*24; var difference = math.ceil((todate.gettime() - fromdate.gettime() + 1) / oneday); nights.val(difference); } }
html:
<table width="100%" border="0" cellpadding="2"> <tr> <td nowrap>vacation request from</td> <td><input type="text" id="from" name="from" size="28" style="width:194px; /*tag style*/" value="" ><div style="display: none;"> <img id="calimg" src="http://www.mywebsite.com/images/calendar-green.gif" alt="popup" class="trigger"></div> <input type="text" id="to" name="to" size="28" style="width:194px; /*tag style*/" value="" ><div style="display: none;"> <img id="calimg" src="http://www.mywebsite.com/images/calendar-green.gif" alt="popup" class="trigger"> </div></td> <td width="1"> </td> </tr> <tr> <td >total days requested: </td> <td colspan="5"><input type="text" id="nights" name="nights" style="width:50px; /*tag style*/" value="" readonly="readonly"></td> </tr> <tr> <td colspan="6"> </td> </tr> </table>
success! this page helped. so, html same above along updated jquery/javascript , i'm in business.
$(function() { $('#from').datepicker({ showonfocus: false, showtrigger: '#calimg', beforeshowday: $.datepicker.noweekends, pickerclass: 'noprevnext', numberofmonths: 1, dateformat: 'mm-dd-yy', mindate: '0', maxdate: '+1y', onselect: function(datestr) { var min = $(this).datepicker('getdate'); $('#to').datepicker('option', 'mindate', min || '0'); datepicked(); }}); $('#to').datepicker({ showonfocus: false, showtrigger: '#calimg', beforeshowday: $.datepicker.noweekends, pickerclass: 'noprevnext', numberofmonths: 1, dateformat: 'mm-dd-yy', mindate: '0', maxdate: '+1y', onselect: function(datestr) { var max = $(this).datepicker('getdate'); $('#from').datepicker('option', 'maxdate', max || '+1y'); datepicked(); }}); }); var datepicked = function() { var = $('#from'); var = $('#to'); var nights = $('#nights'); var startdate = from.datepicker('getdate') var enddate = to.datepicker('getdate') // validate input if (enddate && startdate) { // calculate days between dates var millisecondsperday = 86400 * 1000; // day in milliseconds startdate.sethours(0,0,0,1); // start after midnight enddate.sethours(23,59,59,999); // end before midnight var diff = enddate - startdate; // milliseconds between datetime objects var days = math.ceil(diff / millisecondsperday); // subtract 2 weekend days every week in between var weeks = math.floor(days / 7); var days = days - (weeks * 2); // handle special cases var startday = startdate.getday(); var endday = enddate.getday(); // remove weekend not removed. if (startday - endday > 1) var days = days - 2; // remove start day if span starts on sunday ends before saturday if (startday == 0 && endday != 6) var days = days - 1 // remove end day if span ends on saturday starts after sunday if (endday == 6 && startday != 0) var days = days - 1 nights.val(days); } }
Comments
Post a Comment