Jquery and Javascript date calculation /Weekdays calculation.
Open sharepoint page in edit mode.
Add content editor webpart to page at the end.
Add below script to content editor webpart.
Open sharepoint page in edit mode.
Add content editor webpart to page at the end.
Add below script to content editor webpart.
// JScript source code <script type="text/javascript" src="http://code.jquery.com/jquery.min.js">script> <script src='/_layouts/SP.js' type='text/javascript'>script> <script type=""text/javascript""> var myItems; var countHolidays = 0; $("input[name='CONTROL_NAME$DateTimeField$DateTimeFieldDate']").focusout(function () { var dtfrom = $("input[name='CONTROL_NAME$DateTimeField$DateTimeFieldDate']").val(); var dtto = $("input[name='CONTROL_NAME$DateTimeField$DateTimeFieldDate']").val(); if (dtfrom.length > 0 && dtto.length > 0) { var DaysDiff; var bussDays = calcBusinessDays(new Date(dtfrom), new Date(dtto)); GetHolidays(dtfrom, dtto); DaysDiff = bussDays - countHolidays; $("#ctlCONTROL_ID_TextField").val(DaysDiff); } }); function calcBusinessDays(dDate1, dDate2) { // input given as Date objects var iWeeks, iDateDiff, iAdjust = 0; if (dDate2 < dDate1) return -1; // error code if dates transposed var iWeekday1 = dDate1.getDay(); // day of week var iWeekday2 = dDate2.getDay(); iWeekday1 = (iWeekday1 == 0) ? 7 : iWeekday1; // change Sunday from 0 to 7 iWeekday2 = (iWeekday2 == 0) ? 7 : iWeekday2; if ((iWeekday1 > 5) && (iWeekday2 > 5)) iAdjust = 1; // adjustment if both days on weekend iWeekday1 = (iWeekday1 > 5) ? 5 : iWeekday1; // only count weekdays iWeekday2 = (iWeekday2 > 5) ? 5 : iWeekday2; 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 into account both days on weekend return (iDateDiff + 1); // add 1 because dates are inclusive } // this function will find date difference in days. function daydiff(first, second) { var start = new Date(first); var end = new Date(second); var diff = new Date(end - start); return (diff) / (1000 * 60 * 60 * 24) } function GetHolidays(fromdate, todate) { var myQueryString = '+ todate + ' ' + fromdate + ''; var myContext = new SP.ClientContext.get_current(); var myWeb = myContext.get_web(); var myList = myWeb.get_lists().getByTitle('Holidays'); var myQuery = new SP.CamlQuery(); myQuery.set_viewXml(myQueryString); myItems = myList.getItems(myQuery); myContext.load(myItems, 'Include(ID, Title, Date, Year)'); myContext.executeQueryAsync(Function.createDelegate( this, GetHolidaysSuccess), Function.createDelegate( this, GetHolidaysFail)); } function GetHolidaysFail(sender, args) { //alert('GetEmployeesByAge() failed:' + args.get_message()); //return false; } function GetHolidaysSuccess(sender, args) { var HolidaysCount = myItems.get_count(); var currYear = (new Date).getFullYear(); var holidaysEnumerator = myItems.getEnumerator(); var holidaysDetails = ''; // Loop through all items while (holidaysEnumerator.moveNext()) { // Get current item var currentHoliday = holidaysEnumerator.get_current(); if (currentHoliday.get_item("Year") == currYear) { countHolidays = countHolidays + 1; } } } script> '
0 comments:
Post a Comment