Skip to main content
Participant
April 25, 2023
Question

Calculate difference between 2 dates excluding weekends

  • April 25, 2023
  • 1 reply
  • 1192 views

I am trying to create a javascript that calculates the total days between Start Date and End Date, while excluding weekends (Saturday and Sunday).

 

I used the following formula for the date difference:

 

var sDate1=this.getField("Date2_af_date").value;
var sDate2=this.getField("Date3_af_date").value;
var oDate1 = util.scand("mm/dd/yy", sDate1);
var oDate2 = util.scand("mm/dd/yy", sDate2);

var nDay = 1000 * 60 * 60 * 24;

var eDays=Math.abs(oDate1-oDate2)/nDay+1;
event.value=Math.round(eDays);

 

Having issues figuring out how to exclude weekends. I'm not familiar with Javascript, so any help is appreciated.

This topic has been closed for replies.

1 reply

try67
Community Expert
Community Expert
April 25, 2023

There are several ways you can do it.

One would be to use the getDay method of both Date objects to figure out which days they are in the week, and then based on the number of weeks between them calculate how many weekends there are. For example, if the first day is a Monday and the last day is Wednesday and there are 44 days between them then the result is 12: Math.floor(44/7)*2

So you deduct that result from the total to get the new total: 44-12 = 32 non-weekend days