Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Calculate difference between 2 dates excluding weekends

New Here ,
Apr 25, 2023 Apr 25, 2023

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.

TOPICS
How to , JavaScript , PDF forms
1.2K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 25, 2023 Apr 25, 2023
LATEST

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

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