Finding Difference between Dates in Javascript ( Yui Calendar ) -
i have been working orangehrm project. uses yui calendar. have been working on leave module. leave module lets user choose , date - time frame wants apply leave !
i want show number of working days once user has chosen dates . simple subtract statement not work because want exclude saturdays , sundays equations since don't count working days !
function filltodate(frmleaveapp) { var fromdate = frmleaveapp.elements['txtleavefromdate']; var todate = frmleaveapp.elements['txtleavetodate']; var result = frmleaveapp.elements['txtleavetotalday']; if(!fromdate || !todate || !result) { return; } var = fromdate.value; var b = todate.value; var c = a.split('-'); var d = b.split('-'); var ac = new date(); ac.setfullyear(c[2], c[1], c[0]); var bd = new date(); bd.setfullyear(d[2], d[1], d[0]); result.value = (bd.gettime() - ac.gettime()) / (60*60*24*1000); }
how do ? in advance !
not super proud of solution - must more elegant ways, should work - not handle national holidays yet
function filltodate(frmleaveapp) { var fromdate = frmleaveapp.elements['txtleavefromdate']; var todate = frmleaveapp.elements['txtleavetodate']; var result = frmleaveapp.elements['txtleavetotalday']; if(!fromdate || !todate || !result) { return; } var = new date(fromdate.value.replace(/\-/g,"/")); var = new date(todate.value.replace(/\-/g,"/")); // brute force seems quickest find var res = 0; var aday = 24*60*60*1000; (var = from.gettime(), n=to.gettime();i < n; i+=aday) { var d = new date(i).getday(); if (d!=0 && d !=6) res++; } result.value = res; }
Comments
Post a Comment