# Calculating date difference in weeks w/ leap years

New Here ,
Jun 14, 2019 Jun 14, 2019

Copy link to clipboard

Copied

Hi all,

I don't actually know how to write brand new code in JavaScript and make it work. I've benefited tremendously by having a basic understanding of what I'm doing from a VB class I took in 2005 and from forums like this allowing me to cobble together pieces to do what I'm trying to do. I'm stumped with this one.

I'm in Human Resources. We have a form that people use to request temporary pay increases with a start and end date.

Because of the idiosyncratic systems we use, I'm using the following to calculate the number of work days between the two dates to convert them into pay periods of 10 work days. Holidays are irrelevant to these calculations as they are still considered in the pay period. We have 26.1 pay periods per year. I need the number of pay periods to correctly calculate the extrapolated salary that a temporary pay increase would be over the 26.1 annual pay periods.

The calculations work exactly as they're supposed to. Or they were...until we started getting into temporary increases that end after February 28, 2020. Instead of correctly calculating 26.1 pay periods if I enter in a full year, I end up with 26.2.

I only need to account for one leap year on this form as we will be changing systems before the 2024 leap year (hopefully).

Any help with where I should account for the leap year in the script below? Thanks for your help!

Number of Work Days in Weeks (NumWorkDays)Field

// get the end date value - Date 1

var cEnd = this.getField("EndDate").value

// get the start date value Date 2

var cStart = this.getField("StartDate").value;

var dStart = util.scand("m/d/yyyy H:MM:SS", cStart + " 0:00:00");

var dEnd = util.scand("m/d/yyyy H:MM:SS", cEnd + " 0:00:00");

event.value = calcBusinessDays(dEnd, dStart);

function calcBusinessDays(dEnd, dStart) { // input given as Date objects

var iWeeks, iDateDiff, iAdjust = 0;

if (dEnd < dStart) return -1; // error code if dates transposed

var iWeekday1 = dStart.getDay(); // day of week

var iWeekday2 = dEnd.getDay();

iWeekday1 = (iWeekday1 == 0) ? 7 : iWeekday1; // change Sunday from 0 to 7

iWeekday2 = (iWeekday2 == 0) ? 7 : iWeekday2;

if ((iWeekday1 > 5) && (iWeekday2 > 5)) iAdjust = 1; // adjustment if both days on weekend

iWeekday1 = (iWeekday1 > 5) ? 5 : iWeekday1; // only count weekdays

iWeekday2 = (iWeekday2 > 5) ? 5 : iWeekday2;

//get ms between UTC dates and make into "difference" date

var nYears = iWeekday2.

// calculate differnece in weeks (1000mS * 60sec * 60min * 24hrs * 7 days = 604800000)

iWeeks = Math.floor((dEnd.getTime() - dStart.getTime()) / 604800000)

if (iWeekday1 <= iWeekday2) {

iDateDiff = (iWeeks * 5) + (iWeekday2 - iWeekday1)

} else {

iDateDiff = ((iWeeks + 1) * 5) - (iWeekday1 - iWeekday2)

}

iDateDiff -= iAdjust // take into account both days on weekend

return (iDateDiff + 1); // add 1 because dates are inclusive

}

Number of Pay Periods Field

var v2 = this.getField("EndDate").value;

var v3 = this.getField("NumWorkDays").value;

var v = v3/10;

// clear  displayed result

event.value = "";

if(!isNaN(v) && v2 != 0){

event.value = v;

} else {

event.value = 26.1;

}

TOPICS
Acrobat SDK and JavaScript

Views

120

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more  1 Reply 1
New Here ,
Jun 19, 2019 Jun 19, 2019

Copy link to clipboard

Copied

LATEST

I cleaned up my code a bit...hypothetically....and moved the +1 to account for the inclusive day to an earlier point in the code and it fixed my problem! Sharing here if anyone else runs into the same issue. var cEnd = this.getField("EndDate").value // get the end date value  Date 1

var cStart = this.getField("StartDate").value; // get the start date value Date 2

var dStart = util.scand("mm/dd/yyyy", cStart);

var dEnd = util.scand("mm/dd/yyyy", cEnd);

event.value = calcBusinessDays(dEnd, dStart);

function calcBusinessDays(dEnd, dStart) { // input given as Date objects

var iWeeks, iDateDiff, iAdjust = 0;

if (dEnd < dStart) return -1; // error code if dates transposed

var iWeekday1 = dStart.getDay(); // day of week

var iWeekday2 = dEnd.getDay();

iWeekday1 = (iWeekday1 == 0) ? 7 : iWeekday1; // change Sunday from 0 to 7

iWeekday2 = (iWeekday2 == 0) ? 7 : iWeekday2;

if ((iWeekday1 > 5) && (iWeekday2 > 5)) iAdjust = 1; // adjustment if both days on weekend

iWeekday1 = (iWeekday1 > 5) ? 5 : iWeekday1; // only count weekdays

iWeekday2 = (iWeekday2 > 5) ? 5 : iWeekday2;

iWeeks = Math.floor(((dEnd - dStart) + 1) / 604800000) // calculate difference in weeks  (1000ms*60sec*60min*24hrs*7 days)

if (iWeekday1 <= iWeekday2) {

iDateDiff = (iWeeks * 5) + (iWeekday2 - iWeekday1)

} else {

iDateDiff = ((iWeeks + 1) * 5) - (iWeekday1 - iWeekday2)

}

iDateDiff -= iAdjust // take into account both days on weekend

return (iDateDiff);

}

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more  