Highlighted

Calculate Days of Each Year in Date Range

Avatar

Avatar

redbaron07

Avatar

redbaron07

redbaron07

03-09-2017

Building a budget form where I would like to pro-rate the budgeted amounts by calendar days in the year.  Rather than asking the user to specify the start and end date for each calendar year to which the budget applied, I have built in only the overall start date and end date.  The challenge has been calculating the number of days in each calendar year that fall within the range and adding these to separate numeric fields.  I was thinking there might be a need to manufacture start and end dates for each year and then calculate from there using the code below.  Anyone run into a similar issue or have any potential solution directions they could point me toward? Thanks in advance.

/transform fields to dates

   var regularDate1 = new Date(EndDate.rawValue.replace(/-/g, "/"));

   var regularDate2 = new Date(StartDate.rawValue.replace(/-/g, "/"));

  

   // Get difference between dates in milliseconds

   var milliseconds = regularDate1.getTime() - regularDate2.getTime();

  

   // Define number of milliseconds in one day

   var nMilliSecondsPerDay = 24 * 60 * 60 * 1000;

   // Get difference in days

   var days = Math.floor(milliseconds / nMilliSecondsPerDay) + 1;

   //Display value in Numeric1 field

   DaysYR1.rawValue = days;