Had a request to create a deadline due date utilizing business hours. Here’s the script. I’m still using the moment.js library for other due date calculations. I’ve also included code here to update the HH:MM blocks of the Nintex Date/Time field.
NWF.FormFiller.Events.RegisterAfterReady(function () {
function parseDate(s) {
var b = s.split(/\D/);
return new Date(b[0], b[1] - 1, b[2], b[3] || 0, b[4] || 0);
}
NWF$("#" + dline).change(function (evt) {
switch (evt.target.value) {
case "Expedited":
var d = new Date();
var nwDate = moment(d).add('days', 1);
var newrd = moment(nwDate).format('MM/DD/YYYY');
var now = moment(d);
var htime = moment(now.hour()).format('hh');
var mtime = now.minutes();
var df = NWF$('#' + ddate);
var hourddl = df.parent().find("select[id*='DateHours']");
var minddl = df.parent().find("select[id*='DateMinutes']");
hourddl.val(htime);
var rMinutes = 5 * (Math.floor(Math.abs(mtime / 5)));
rMinutes = ("00" + rMinutes).substr(-2, 2);
minddl.val(rMinutes);
NWF$('#' + ddate).val(newrd);
NWF$('#' + ddate).change();
break;
case "Standard":
var startHour = 8;
var endHour = 17;
var weekendDays = {
'6': 2,
'0': 1
};
var date = new Date();
var hrsPerDay = endHour - startHour;
var hrsToAdd,
hrsLeftToday;
hours = 8;
if (date.getHours() < startHour) {
date.setHours(startHour, 0, 0, 0);
} else if (date.getHours() > endHour) {
date.setDate(date.getDate() + 1);
date.setHours(startHour, 0, 0, 0);
}
if (date.getDay()in weekendDays) {
date.setDate(date.getDate() + weekendDays[date.getDay()]);
date.setHours(startHour, 0, 0, 0);
}
while (hours) {
hrsLeftToday = endHour - date.getHours();
if (!hrsLeftToday && hours) {
date.setDate(date.getDate() + 1);
date.setHours(startHour);
}
hrsToAdd = hrsLeftToday > hours ? hours : hrsLeftToday;
hours -= hrsToAdd;
if (hrsToAdd == hrsPerDay && hours) {
date.setDate(date.getDate() + 1);
} else {
date.setHours(date.getHours() + hrsToAdd);
}
if (!hours) {
if (date.getHours() > endHour) {
date.setDate(date.getDate() + 1);
date.setHours(startHour + date.getHours() - endHour);
}
if (date.getHours() == endHour && (date.getMinutes() || date.getSeconds())) {
date.setDate(date.getDate() + 1);
date.setHours(startHour);
}
}
if (date.getDay()in weekendDays) {
date.setDate(date.getDate() + weekendDays[date.getDay()]);
}
}
var nwDate = moment(date).format('MM/DD/YYYY');
var now = moment(date);
var htime = now.hour();
var mtime = now.minutes();
NWF$('#' + ddate).val(nwDate);
var df = NWF$('#' + ddate);
var hourddl = df.parent().find("select[id*='DateHours']");
var minddl = df.parent().find("select[id*='DateMinutes']");
hourddl.val(htime);
var rMinutes = 5 * (Math.floor(Math.abs(mtime / 5)));
rMinutes = ("00" + rMinutes).substr(-2, 2);
minddl.val(rMinutes);
NWF$('#' + ddate).change();
}
});
});