I am new to javascript.
I have specific month columns (9/30/2022, 10/31/2022,11/30/2022). I have a contract with a start date and end date (spanning multiple months).
I need to determine the number of days the contract was active for a specific month column.
Example:
Contact Start Date: 09/15/2022
Contract End Date: 10/24/2022
Number of days the contract was active in Sept 2022 is 16.
I found the code below that gives me the contract period broken down for each month (i.e.) **9 - 17; 10 - 23; **
Thank you in advance for any assistance.
I found this code
function getDays() { var dropdt = new Date(document.getElementById("arr").value); var pickdt = new Date(document.getElementById("dep").value); var result = ""; for (var year = dropdt.getFullYear(); year <= pickdt.getFullYear(); year++) { var firstMonth = (year == dropdt.getFullYear()) ? dropdt.getMonth() : 0; var lastMonth = (year == pickdt.getFullYear()) ? pickdt.getMonth() : 11; for (var month = firstMonth; month <= lastMonth; month++) { var firstDay = (year === dropdt.getFullYear() && month === firstMonth) ? dropdt.getDate() : 1; var lastDay = (year === pickdt.getFullYear() && month === lastMonth) ? pickdt.getDate() : 0; var lastDateMonth = (lastDay === 0) ? (month + 1) : month var firstDate = new Date(year, month, firstDay); var lastDate = new Date(year, lastDateMonth, lastDay); result += (month + 1) + " - " + parseInt((lastDate - firstDate) / (24 * 3600 * 1000) + 1) + "; "; } } return result; } function cal() { if (document.getElementById("dep")) { document.getElementById("number-of-dates").value = getDays(); }
Calculate `