javascript - Calculate a date in the future, skipping weekends and holidays, using JS/jQuery -
i trying script display 3 dates in future, estimating arrival time if customer orders today. in advertising , on homepage want show 4, 10 , 12 business days in future, formatted "monday, january 12th", allow customers know approximately when might labels.
i able 3 dates in future, not accounting weekends or of dates in 'holiday date array,'
i need count 4, 10, , 12 business days future., code shown below counting days without skipping weekends or holidays.
what doing wrong?
my current js / jquery code:
var natdays = [ [1, 1, 'new year'], [1, 20, 'martin luther king'], [2, 17, 'washingtons birthday'], [5, 26, 'memorial day'], [7, 4, 'independence day'], [9, 12, 'labour day'], [10, 14, 'columbus day'], [11, 11, 'veterans day'], [11, 28, 'thanks giving day'], [12, 25, 'christmas'] ]; // datemin minimum delivery date var datemin = new date(); datemin.setdate(datemin.getdate() + (datemin.gethours() >= 14 ? 1 : 0)); function addbusinessdays(curdate, weekdaystoadd) { var date = new date(curdate.gettime()); while (weekdaystoadd > 0) { date.setdate(date.getdate() + 1); //check if current day business day if (noweekendsorholidays(date)) { weekdaystoadd--; } } return date; } function noweekendsorholidays(date) { var noweekend = jquery.datepicker.noweekends(date); return (noweekend[0] ? nationaldays(date) : noweekend); } function nationaldays(date) { (i = 0; < natdays.length; i++) { if (date.getmonth() == natdays[i][0] - 1 && date.getdate() == natdays[i][1]) { return [false, natdays[i][2] + '_day']; } } return [true, '']; } function setdeliverydate(date) { jquery('#normal-date').text(jquery.datepicker.formatdate('dd, mm dd', date)); } function setrushdeliverydate(date) { jquery('#rush-date').text(jquery.datepicker.formatdate('dd, mm dd', date)); }function setsuperdeliverydate(date) { jquery('#superrush-date').text(jquery.datepicker.formatdate('dd, mm dd', date)); } setdeliverydate(addbusinessdays(datemin, 12)); setrushdeliverydate(addbusinessdays(datemin, 10)); setsuperdeliverydate(addbusinessdays(datemin, 4)); ;
html:
<p class="delvdate">guaranteed <span id="normal-date"></span>*</p> <p class="delvdate">guaranteed <span id="rush-date"></span>*</p> <p class="delvdate">guaranteed <span id="superrush-date"></span>*</p>
Comments
Post a Comment