{"id":718,"date":"2021-12-07T15:53:52","date_gmt":"2021-12-07T20:53:52","guid":{"rendered":"http:\/\/markwheeler.com\/?p=718"},"modified":"2021-12-07T15:53:52","modified_gmt":"2021-12-07T20:53:52","slug":"calculate-business-hours-in-nintex-form","status":"publish","type":"post","link":"https:\/\/www.markwheeler.com\/?p=718","title":{"rendered":"Calculate Business Hours in Nintex Form"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Had a request to create a deadline due date utilizing business hours. Here&#8217;s the script. I&#8217;m still using the moment.js library for other due date calculations. I&#8217;ve also included code here to update the HH:MM blocks of the Nintex Date\/Time field. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>NWF.FormFiller.Events.RegisterAfterReady(function () {\n    function parseDate(s) {\n        var b = s.split(\/\\D\/);\n        return new Date(b&#91;0], b&#91;1] - 1, b&#91;2], b&#91;3] || 0, b&#91;4] || 0);\n    }\n    NWF$(\"#\" + dline).change(function (evt) {\n        switch (evt.target.value) {\n        case \"Expedited\":\n            var d = new Date();\n            var nwDate = moment(d).add('days', 1);\n            var newrd = moment(nwDate).format('MM\/DD\/YYYY');\n            var now = moment(d);\n            var htime = moment(now.hour()).format('hh');\n            var mtime = now.minutes();\n            var df = NWF$('#' + ddate);\n            var hourddl = df.parent().find(\"select&#91;id*='DateHours']\");\n            var minddl = df.parent().find(\"select&#91;id*='DateMinutes']\");\n            hourddl.val(htime);\n            var rMinutes = 5 * (Math.floor(Math.abs(mtime \/ 5)));\n            rMinutes = (\"00\" + rMinutes).substr(-2, 2);\n            minddl.val(rMinutes);\n            NWF$('#' + ddate).val(newrd);\n            NWF$('#' + ddate).change();\n            break;\n        case \"Standard\":\n            var startHour = 8;\n            var endHour = 17;\n            var weekendDays = {\n                '6': 2,\n                '0': 1\n            };\n            var date = new Date();\n            var hrsPerDay = endHour - startHour;\n            var hrsToAdd,\n            hrsLeftToday;\n            hours = 8;\n            if (date.getHours() &lt; startHour) {\n                date.setHours(startHour, 0, 0, 0);\n            } else if (date.getHours() > endHour) {\n                date.setDate(date.getDate() + 1);\n                date.setHours(startHour, 0, 0, 0);\n            }\n            if (date.getDay()in weekendDays) {\n                date.setDate(date.getDate() + weekendDays&#91;date.getDay()]);\n                date.setHours(startHour, 0, 0, 0);\n            }\n            while (hours) {\n                hrsLeftToday = endHour - date.getHours();\n                if (!hrsLeftToday &amp;&amp; hours) {\n                    date.setDate(date.getDate() + 1);\n                    date.setHours(startHour);\n                }\n                hrsToAdd = hrsLeftToday > hours ? hours : hrsLeftToday;\n                hours -= hrsToAdd;\n                if (hrsToAdd == hrsPerDay &amp;&amp; hours) {\n                    date.setDate(date.getDate() + 1);\n                } else {\n                    date.setHours(date.getHours() + hrsToAdd);\n                }\n                if (!hours) {\n                    if (date.getHours() > endHour) {\n                        date.setDate(date.getDate() + 1);\n                        date.setHours(startHour + date.getHours() - endHour);\n                    }\n                    if (date.getHours() == endHour &amp;&amp; (date.getMinutes() || date.getSeconds())) {\n                        date.setDate(date.getDate() + 1);\n                        date.setHours(startHour);\n                    }\n                }\n                if (date.getDay()in weekendDays) {\n                    date.setDate(date.getDate() + weekendDays&#91;date.getDay()]);\n                }\n            }\n            var nwDate = moment(date).format('MM\/DD\/YYYY');\n            var now = moment(date);\n            var htime = now.hour();\n            var mtime = now.minutes();\n            NWF$('#' + ddate).val(nwDate);\n            var df = NWF$('#' + ddate);\n            var hourddl = df.parent().find(\"select&#91;id*='DateHours']\");\n            var minddl = df.parent().find(\"select&#91;id*='DateMinutes']\");\n            hourddl.val(htime);\n            var rMinutes = 5 * (Math.floor(Math.abs(mtime \/ 5)));\n            rMinutes = (\"00\" + rMinutes).substr(-2, 2);\n            minddl.val(rMinutes);\n            NWF$('#' + ddate).change();\n        }\n\n    });\n});\n    \n                \n\n  <\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Had a request to create a deadline due date utilizing business hours. Here&#8217;s the script. I&#8217;m still using the moment.js library for other due date calculations. I&#8217;ve also included code here to update the HH:MM blocks of the Nintex Date\/Time field.<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"footnotes":""},"categories":[2,10,11],"tags":[73],"class_list":["post-718","post","type-post","status-publish","format-standard","hentry","category-computer","category-sharepoint","category-software","tag-nintex-forms"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/www.markwheeler.com\/index.php?rest_route=\/wp\/v2\/posts\/718","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.markwheeler.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.markwheeler.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.markwheeler.com\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.markwheeler.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=718"}],"version-history":[{"count":0,"href":"https:\/\/www.markwheeler.com\/index.php?rest_route=\/wp\/v2\/posts\/718\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.markwheeler.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=718"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.markwheeler.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=718"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.markwheeler.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=718"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}