function formatDate(datSource,sFormat = 'y-m-d', sSeparator = "-") { var sResult = ""; if (typeof datSource == 'object') { var sYear = datSource.getFullYear().toString(); var sMonth = "0" + (datSource.getMonth() + 1).toString(); sMonth = sMonth.substring(sMonth.length - 2); var sDay = "0" + datSource.getDate().toString(); sDay = sDay.substring(sDay.length - 2); for (var sFormatItem of sFormat.split(sSeparator)) { if (sResult.length > 0) sResult = sResult + sSeparator; switch (sFormatItem.toUpperCase()) { case "Y": case "A": sResult = sResult + sYear; break; case "M": sResult = sResult + sMonth; break; case "D": sResult = sResult + sDay; break; } } } return sResult; } function setDateValues(sIDControlsCalendar, datMin = undefined, datMax = undefined, sControlIndetifier = "#") { loDateMax = (datMax == undefined || datMax == null) ? new Date() : datMax; loDateMin = (datMin == undefined || datMin == null) ? new Date() : datMin; var sDatMin = ""; if (datMin != undefined && (typeof datMin != 'string' || datMin.length > 0)) { if (typeof datMin == 'string' || typeof datMin == 'number') { loDateMin = new Date(); loDateMin.setDate(loDateMax.getDate() - datMin); } else loDateMin = datMin; sDatMin = formatDate(loDateMin); } else sDatMin = formatDate(loDateMin); var sDatMax = ""; if (datMax != undefined && (typeof datMax != 'string' || datMax.length > 0)) { if (typeof datMax == 'string' || typeof datMax == 'number') { loDateMax = new Date(); loDateMax.setDate(loDateMin.getDate() + datMax); } else loDateMax = datMax; sDatMax = formatDate(loDateMax); } else sDatMax = formatDate(loDateMax); for (var oControlCalendar of document.querySelectorAll(sControlIndetifier + sIDControlsCalendar.split(".").join("|").split(",").join("|").split(";").join("|").split(" ").join("").split("|").join(", " + sControlIndetifier))) { if (sDatMin.length > 0) oControlCalendar.min = sDatMin; if (sDatMax.length > 0) oControlCalendar.max = sDatMax; } } function configControlDateFronTo(sIDControlCalendarFrom, sIDControlCalendarTo, datMin = undefined, datMax = undefined) { var loControlFrom = (typeof sIDControlCalendarFrom == "string") ? document.getElementById(sIDControlCalendarFrom.trim()) : sIDControlCalendarFrom; var loControlTo = (typeof sIDControlCalendarTo == "string") ? document.getElementById(sIDControlCalendarTo.trim()) : sIDControlCalendarTo; setDateValues(loControlFrom.id + "," + loControlTo.id, datMin, datMax); loControlFrom.value = loControlFrom.min; loControlFrom.addEventListener('change', function(e) { setCalendarFromTo(loControlFrom.id, loControlTo.id); }); loControlTo.value = loControlTo.max; loControlTo.addEventListener('change', function(e) { setCalendarFromTo(loControlFrom, loControlTo); }); } function setCalendarFromTo(sIDControlCalendarFrom, sIDControlCalendarTo) { var oControlCalendarFrom = (typeof sIDControlCalendarFrom == "string") ? document.getElementById(sIDControlCalendarFrom.trim()) : sIDControlCalendarFrom; var oControlCalendarTo = (typeof sIDControlCalendarTo == "string") ? document.getElementById(sIDControlCalendarTo.trim()) : sIDControlCalendarTo; var sValueFrom = oControlCalendarFrom.value; var sValueTo = oControlCalendarTo.value; if (sValueFrom.length > 0) oControlCalendarTo.min = sValueFrom; if (sValueTo.length > 0) oControlCalendarFrom.max = sValueTo; }