# Get Working Days Between 2 Dates

**URL:** <https://forum.servoy.com/t/get-working-days-between-2-dates/11136>\
**Category:** Classic Servoy\
**Created:** [September 10, 2009, 3:25pm UTC](https://forum.servoy.com/t/get-working-days-between-2-dates/11136 "2009-09-10T15:25:46Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Foobrother](https://avatars.discourse-cdn.com/v4/letter/f/50afbb/32.png) [@Foobrother](https://forum.servoy.com/u/Foobrother)\
**Post date:** [September 10, 2009, 3:25pm UTC](https://forum.servoy.com/t/get-working-days-between-2-dates/11136/1 "2009-09-10T15:25:46Z")

</div>

Hi everybody,

I’m trying to implement a function to calculate the number of working days between 2 dates.  
As I’m more a Java programmer than Javascript one I naturally decided to write it in Java. Moreover I found [a quite simple solution in Java](http://forums.sun.com/thread.jspa?messageID=4285911#4285911) which should be quite easy to integrate using Java packages in Servoy Developer.

Here is the function:

```auto
function GetWorkingDays()
{
	var v_calendar_package = Packages.java.util.Calendar;
	var v_calendar = new v_calendar_package.getInstance();
	var v_date_from = new Packages.java.util.Date(arguments[0]);
	v_calendar.setTimeInMillis(arguments[1]);
	var v_date_to = v_calendar.getTime();

	if (v_date_from == null || v_date_to == null) {
		return 0;
	}
	if (v_date_from.after(v_date_to)) {
		var v_temp = v_date_from;
		v_date_from = v_date_to;
		v_date_to = v_temp;
	}
	var v_calFrom = new v_calendar_package.getInstance();
	v_calFrom.setTime(v_date_from);
	var v_calTo = new v_calendar_package.getInstance();
	v_calTo.setTime(v_date_to);
	
	var v_iNoOfWorkingDays = 0;
	do {
		if (v_calFrom.get(v_calendar_package.DAY_OF_WEEK) != v_calendar_package.SATURDAY
		&& v_calFrom.get(v_calendar_package.DAY_OF_WEEK) != v_calendar_package.SUNDAY) {
			v_iNoOfWorkingDays++;
		}
		v_calFrom.add(v_calendar_package.DATE, 1);
	} while (v_calFrom.getTimeInMillis() < v_calTo.getTimeInMillis());
	
	application.output("Working days between "+arguments[0]+" and "+arguments[1]+" = "+v_iNoOfWorkingDays+" days");
	return v_iNoOfWorkingDays;	
}

```

My first issue is to convert the date from JavaScript to Java on this line:

```auto
var v_date_from = new Packages.java.util.Date(arguments[0]);

```

I get an error because Java doesn’t seem to like a date in JavaScript?! When I print the dates (arg0 and arg1) they look like that “Fri Sep 18 00:00:00 BST 2009”.  
I have tried to convert them in millisec and use the Java Date constructor with millisec but I get the following error:

> Cannot convert Thu Oct 15 00:00:00 BST 2009 to java.lang.Long

My code was ```  
var v\_date\_from = new Packages.java.util.Date(arguments[0].getTime());

```auto

I could parse the dates to extract the year, month and day but I find it quite annoying and would like to know if there is not an easier way to do it?

Maybe it can even be done in full Javascript but unfortunately I'm not an expert <img src="{SMILIES_PATH}/icon_sad.gif" alt=":(" title="Sad" />

Btw if you see any other problems in this code (I'm sure there are much more), feel free to tell me <img src="{SMILIES_PATH}/icon_wink.gif" alt=";-)" title="Wink" />
So I would like to know
```

---

<div class="post-metadata">

**Author:** ![Foobrother](https://avatars.discourse-cdn.com/v4/letter/f/50afbb/32.png) [@Foobrother](https://forum.servoy.com/u/Foobrother)\
**Post date:** [September 11, 2009, 8:30am UTC](https://forum.servoy.com/t/get-working-days-between-2-dates/11136/2 "2009-09-11T08:30:34Z")

</div>

To progress I’ve used the Java Date constructor with Year, Month, Day. It works to create Date objects ![:-)]( "Smile")  
However I don’t seem to be able to call a method from my Date object ?!

My Date object is declared like that:

```auto
var v_date_from = new Packages.java.util.Date(v_temp_date_year, v_temp_date_month, v_temp_date_day);

```

Here is the code where I call a method:

```auto
if (v_date_from.after(v_date_to)) {
...

```

Error I get:

> TypeError: Cannot find function after.

However this method exists: [http://java.sun.com/j2se/1.5.0/docs/api … .util.Date](http://java.sun.com/j2se/1.5.0/docs/api/java/util/Date.html#after(java.util.Date))

Any idea?

Here is the full code updated:

```auto
function GetWorkingDays()
{
	var v_calendar_package = Packages.java.util.Calendar;
	var v_calendar = new v_calendar_package.getInstance();
	var v_temp_date_year = utils.dateFormat(arguments[0],'yyyy');
	var v_temp_date_month = utils.dateFormat(arguments[0],'MM');
	var v_temp_date_day = utils.dateFormat(arguments[0],'dd');
	var v_date_from = new Packages.java.util.Date(v_temp_date_year, v_temp_date_month, v_temp_date_day);
	
	v_temp_date_year = utils.dateFormat(arguments[1],'yyyy');
	v_temp_date_month = utils.dateFormat(arguments[1],'MM');
	v_temp_date_day = utils.dateFormat(arguments[1],'dd');
	v_calendar.set(v_temp_date_year, v_temp_date_month, v_temp_date_day);
	var v_date_to = v_calendar.getTime();

	if (v_date_from == null || v_date_to == null) {
		return 0;
	}
	if (v_date_from.after(v_date_to)) {
		var v_temp = v_date_from;
		v_date_from = v_date_to;
		v_date_to = v_temp;
	}
	var v_calFrom = new v_calendar_package.getInstance();
	v_calFrom.setTime(v_date_from);
	var v_calTo = new v_calendar_package.getInstance();
	v_calTo.setTime(v_date_to);
	
	var v_iNoOfWorkingDays = 0;
	do {
		if (v_calFrom.get(v_calendar_package.DAY_OF_WEEK) != v_calendar_package.SATURDAY
		&& v_calFrom.get(v_calendar_package.DAY_OF_WEEK) != v_calendar_package.SUNDAY) {
			v_iNoOfWorkingDays++;
		}
		v_calFrom.add(v_calendar_package.DATE, 1);
	} while (v_calFrom.getTimeInMillis() < v_calTo.getTimeInMillis());
	
	application.output("Working days between "+arguments[0]+" and "+arguments[1]+" = "+v_iNoOfWorkingDays+" days");
	return v_iNoOfWorkingDays;	
}

```

---

<div class="post-metadata">

**Author:** ![rafig](https://yyz2.discourse-cdn.com/flex010/user_avatar/forum.servoy.com/rafig/32/4463_2.png) [@rafig](https://forum.servoy.com/u/rafig)\
**Post date:** [September 11, 2009, 9:46am UTC](https://forum.servoy.com/t/get-working-days-between-2-dates/11136/3 "2009-09-11T09:46:37Z")

</div>

Hi,  
I’ve been doing a lot of stuff with dates and have these globals I call, the first gives you what you want

```auto
// calculateworkingdays(startDate, endDate)

var stDate = new Date(arguments[0]); 
var enDate = new Date(arguments[1]); 
var startDateYear = stDate.getFullYear(); 
var startDateMonth = stDate.getMonth(); 
var startDateDat = stDate.getDate(); 
var endDateYear = enDate.getFullYear(); 
var endDateMonth = enDate.getMonth(); 
var endDateDat = enDate.getDate();

var ssDate = new Date(startDateYear,startDateMonth,startDateDat); 
var eeDate = new Date(endDateYear,endDateMonth,endDateDat);

if (ssDate > eeDate) 
{ 
	return -1; 
}

var workingdays = 0 ; 

while (ssDate <= eeDate) 
{ 
	if(ssDate.getDay()!=0 && ssDate.getDay()!=6) 
	{ 
		workingdays++; 
	} 
	//ssDate = new Date(ssDate.valueof()+86400000); 
	ssDate = new Date(ssDate.getFullYear(), ssDate.getMonth(), ssDate.getDate() + 1 ) ;
}
 
return workingdays - 1 ;

```

call it like this

```auto
no_days = globals.calculateWorkingDays( start_date , end_date ) ;

```

I also needed to be able to add & subtract a certain number of workings days to/from a date

```auto
// this routine will add working days to a date
// myDate = startingdate, days = no. working days to add.

var myDate = arguments[0] ;
var days = arguments[1] ;
if(!myDate) return ;

var temp_date = new Date() ;
var i = 0 ;
var days_to_add = 0 ;
while ( i < ( days ) )
{
	days_to_add += 1 ;
	temp_date = new Date( myDate.getTime() + (days_to_add * 24 * 60 * 60 * 1000) );
	// 0 = Sunday, 6 = Saturday, if the date not equals a weekend day then increase by 1
	if ( ( temp_date.getDay() != 0 ) && ( temp_date.getDay() != 6 ) )
	{
		i += 1 ;
	}
}
return new Date( myDate.getTime() + days_to_add * 24 * 60 * 60 * 1000 ) ;

```

call it like this

```auto
end_date = globals.addWorkingDays( start_date , no_days ) ;

```

```auto
// this routine will subtract working days to a date
// myDate = startingdate
// days = no. working days to subtract

var myDate = arguments[0] ;
var days = arguments[1] ;
//need to negate value if it is positive
if ( days > 0 )
{
	// need to make it 1 less
	days -= 1 ;
	days = days * -1 ;
}
else // it's negative, so increment by 1
{
	days += 1 ;
}

if(!myDate) return ;

var temp_date = new Date() ;
var i = 0 ;
var days_to_add = 0 ;
while ( i >= ( days ) )
{
	days_to_add -= 1 ;
	temp_date = new Date( myDate.getTime() + (days_to_add * 24 * 60 * 60 * 1000) );
	// 0 = Sunday, 6 = Saturday, if the date not equals a weekend day then decrease by 1
	if ( ( temp_date.getDay() != 0 ) && ( temp_date.getDay() != 6 ) )
	{
		i -= 1 ;
	}
}
return new Date( myDate.getTime() + days_to_add * 24 * 60 * 60 * 1000 ) ;

```

call it like this

```auto
start_date = globals.subWorkingDays(end_date,no_days) ;

```

Hope this helps

Rafi

---

<div class="post-metadata">

**Author:** ![rafig](https://yyz2.discourse-cdn.com/flex010/user_avatar/forum.servoy.com/rafig/32/4463_2.png) [@rafig](https://forum.servoy.com/u/rafig)\
**Post date:** [September 11, 2009, 9:54am UTC](https://forum.servoy.com/t/get-working-days-between-2-dates/11136/4 "2009-09-11T09:54:26Z")

</div>

BTW,  
I also use the excellent & free DateUtils plug-in from Dr. Maison

> **[Servoy-Plugins.de - DateUtils plugin](http://www.servoy-plugins.de/plugins/dateutils/dateutils-plugin.html)**
>
> Free plugin for easy calculation with dates and times in Servoy methods.

it has lots of useful date functions etc.

Rafi

---

<div class="post-metadata">

**Author:** ![Foobrother](https://avatars.discourse-cdn.com/v4/letter/f/50afbb/32.png) [@Foobrother](https://forum.servoy.com/u/Foobrother)\
**Post date:** [September 11, 2009, 10:23am UTC](https://forum.servoy.com/t/get-working-days-between-2-dates/11136/5 "2009-09-11T10:23:21Z")

</div>

> rafig:  
> BTW,  
> I also use the excellent & free DateUtils plug-in from Dr. Maison  
> [Servoy-Plugins.de - DateUtils plugin](http://www.servoy-plugins.de/plugins/dateutils/dateutils-plugin.html)
> 
> it has lots of useful date functions etc.
> 
> Rafi

Interesting. Thanks for the info.  
I’ve just tried to get it, but the registration form doesn’t seem to post the details. I get a blank page as if it stopped half-way through ![:(]( "Sad")

**EDIT:** Actually it worked! I just received 3 emails with the download link and licence key ![:D]( "Very Happy")

---

<div class="post-metadata">

**Author:** ![patrick](https://avatars.discourse-cdn.com/v4/letter/p/b2d939/32.png) [@patrick](https://forum.servoy.com/u/patrick)\
**Post date:** [September 11, 2009, 10:32am UTC](https://forum.servoy.com/t/get-working-days-between-2-dates/11136/6 "2009-09-11T10:32:49Z")

</div>

Thanks for reporting (empty page)! I will fix this asap.

---

<div class="post-metadata">

**Author:** ![Foobrother](https://avatars.discourse-cdn.com/v4/letter/f/50afbb/32.png) [@Foobrother](https://forum.servoy.com/u/Foobrother)\
**Post date:** [September 11, 2009, 10:45am UTC](https://forum.servoy.com/t/get-working-days-between-2-dates/11136/7 "2009-09-11T10:45:02Z")

</div>

> patrick:  
> Thanks for reporting (empty page)! I will fix this asap.

No problem.

However the plugin will not help me I think, I’m afraid ![:(]( "Sad")  
I need to detect which day (e.g. “Sunday”) it is for a given date. I would need a function like _getDayName(“26-03-2010”)_

It’s this bit in my code:

```auto
	do {
		if (v_calFrom.get(v_calendar_package.DAY_OF_WEEK) != v_calendar_package.SATURDAY
		&& v_calFrom.get(v_calendar_package.DAY_OF_WEEK) != v_calendar_package.SUNDAY) {
			v_iNoOfWorkingDays++;
		}
		v_calFrom.add(v_calendar_package.DATE, 1);
	} while (v_calFrom.getTimeInMillis() < v_calTo.getTimeInMillis());

```

---

<div class="post-metadata">

**Author:** ![ROCLASI](https://yyz2.discourse-cdn.com/flex010/user_avatar/forum.servoy.com/roclasi/32/4371_2.png) [@ROCLASI](https://forum.servoy.com/u/ROCLASI)\
**Post date:** [September 11, 2009, 10:53am UTC](https://forum.servoy.com/t/get-working-days-between-2-dates/11136/8 "2009-09-11T10:53:34Z")

</div>

Hi Foobrother,

> Foobrother:  
> However the plugin will not help me I think, I’m afraid ![:(]( "Sad")  
> I need to detect which day (e.g. “Sunday”) it is for a given date. I would need a function like _getDayName(“26-03-2010”)_

If I am not mistaken Rafi gave you a fully functional method in pure JavaScript. Why don’t you use that ?

---

<div class="post-metadata">

**Author:** ![patrick](https://avatars.discourse-cdn.com/v4/letter/p/b2d939/32.png) [@patrick](https://forum.servoy.com/u/patrick)\
**Post date:** [September 11, 2009, 11:02am UTC](https://forum.servoy.com/t/get-working-days-between-2-dates/11136/9 "2009-09-11T11:02:09Z")

</div>

getDayName() is actually available for DateTime objects in the DateUtils plugin. It delivers “Sunday” if English is your locale.

I could also enhance the plugin to deliver an array of “working” days between two dates. The question is: what is a working day? Monday through Saturday? Should I make Saturday an option? What if you are Muslim or Jewish? Any thoughts?

---

<div class="post-metadata">

**Author:** ![Foobrother](https://avatars.discourse-cdn.com/v4/letter/f/50afbb/32.png) [@Foobrother](https://forum.servoy.com/u/Foobrother)\
**Post date:** [September 11, 2009, 12:12pm UTC](https://forum.servoy.com/t/get-working-days-between-2-dates/11136/10 "2009-09-11T12:12:17Z")

</div>

> ROCLASI:  
> Hi Foobrother,
> 
> If I am not mistaken Rafi gave you a fully functional method in pure JavaScript. Why don’t you use that ?

Oooh yes, I didn’t notice that post! I thought it was my last one with all the quote and and pieces of code ![:oops:]( "Embarassed")  
Will check that.

Thanks for telling me ![:wink:]( "Wink")

> patrick:  
> getDayName() is actually available for DateTime objects in the DateUtils plugin. It delivers “Sunday” if English is your locale.
> 
> I could also enhance the plugin to deliver an array of “working” days between two dates. The question is: what is a working day? Monday through Saturday? Should I make Saturday an option? What if you are Muslim or Jewish? Any thoughts?

Ok, I’ll check this DateTime class.

I understand that including a working days functions is not easy because of all the reasons you have mentioned. When I was searching for some code doing it was the same issue. Some were including both dates in the count others not, others just the start day etc…

---

<div class="post-metadata">

**Author:** ![patrick](https://avatars.discourse-cdn.com/v4/letter/p/b2d939/32.png) [@patrick](https://forum.servoy.com/u/patrick)\
**Post date:** [September 11, 2009, 12:24pm UTC](https://forum.servoy.com/t/get-working-days-between-2-dates/11136/11 "2009-09-11T12:24:07Z")

</div>

What might be useful is to enhance a plugin method called createDateSeries(). That creates an array from start to end. I could add an optional parameter Number daysToExclude. That way you could pass [6, 7] for example and the series created would then not contain Saturdays and Sundays.

---

<div class="post-metadata">

**Author:** ![Foobrother](https://avatars.discourse-cdn.com/v4/letter/f/50afbb/32.png) [@Foobrother](https://forum.servoy.com/u/Foobrother)\
**Post date:** [September 11, 2009, 12:33pm UTC](https://forum.servoy.com/t/get-working-days-between-2-dates/11136/12 "2009-09-11T12:33:50Z")

</div>

> rafig:  
> Hi,  
> I’ve been doing a lot of stuff with dates and have these globals I call, the first gives you what you want
> 
> …
> 
> Hope this helps
> 
> Rafi

Many thanks for your code Rafi!  
It works perfectly, I just modified it to make it include the dateFrom and dateTo in the count.

In was looking for too complicate solution with Java! I didn’t know we could work like that with dates in JavaScript.

Cheers ![:mrgreen:]( "Mr. Green")

---

<div class="post-metadata">

**Author:** ![Foobrother](https://avatars.discourse-cdn.com/v4/letter/f/50afbb/32.png) [@Foobrother](https://forum.servoy.com/u/Foobrother)\
**Post date:** [September 11, 2009, 12:37pm UTC](https://forum.servoy.com/t/get-working-days-between-2-dates/11136/13 "2009-09-11T12:37:05Z")

</div>

> patrick:  
> What might be useful is to enhance a plugin method called createDateSeries(). That creates an array from start to end. I could add an optional parameter Number daysToExclude. That way you could pass [6, 7] for example and the series created would then not contain Saturdays and Sundays.

Yes this solution would be very flexible in fact. You could have a 2 working days week (my dream ![:lol:]( "Laughing") )

---

<div class="post-metadata">

**Author:** ![patrick](https://avatars.discourse-cdn.com/v4/letter/p/b2d939/32.png) [@patrick](https://forum.servoy.com/u/patrick)\
**Post date:** [September 11, 2009, 1:08pm UTC](https://forum.servoy.com/t/get-working-days-between-2-dates/11136/14 "2009-09-11T13:08:54Z")

</div>

OK. The next version of the DateUtils plugin will have a method called getWorkingDays(Date start, Date end, Number nonWorkingDays). You can call it like this:

plugins.DateUtils.getWorkingDays(vStart, vEnd, [5, 6, 7]);

This returns an array containing all days from vStart to vEnd without Fridays, Saturdays and Sundays.

If you want that release before I updated the installer, please send me an email.

---

<div class="post-metadata">

**Author:** ![rafig](https://yyz2.discourse-cdn.com/flex010/user_avatar/forum.servoy.com/rafig/32/4463_2.png) [@rafig](https://forum.servoy.com/u/rafig)\
**Post date:** [September 11, 2009, 2:24pm UTC](https://forum.servoy.com/t/get-working-days-between-2-dates/11136/15 "2009-09-11T14:24:20Z")

</div>

> Foobrother:  
> Many thanks for your code Rafi!  
> It works perfectly, I just modified it to make it include the dateFrom and dateTo in the count.
> 
> In was looking for too complicate solution with Java! I didn’t know we could work like that with dates in JavaScript.
> 
> Cheers ![:mrgreen:]( "Mr. Green")

Glad it works for you ![:D]( "Very Happy")  
I am using normal European Weekend days to exclude (Saturday & Sunday).

Patrick, like you idea for an update to your plug-in! (Are you putting my working days code in it ![:wink:]( "Wink") ? )

Let us know when it’s ready to download.

Rafi

---

<div class="post-metadata">

**Author:** ![patrick](https://avatars.discourse-cdn.com/v4/letter/p/b2d939/32.png) [@patrick](https://forum.servoy.com/u/patrick)\
**Post date:** [September 11, 2009, 2:28pm UTC](https://forum.servoy.com/t/get-working-days-between-2-dates/11136/16 "2009-09-11T14:28:02Z")

</div>

It’s ready now. Just not as a download with installer. Send me an email, I send that to you then.

---

<div class="post-metadata">

**Author:** ![Foobrother](https://avatars.discourse-cdn.com/v4/letter/f/50afbb/32.png) [@Foobrother](https://forum.servoy.com/u/Foobrother)\
**Post date:** [September 11, 2009, 2:40pm UTC](https://forum.servoy.com/t/get-working-days-between-2-dates/11136/17 "2009-09-11T14:40:50Z")

</div>

> patrick:  
> This returns an array containing all days from vStart to vEnd without Fridays, Saturdays and Sundays.

Actually for my case this is not exactly what I use but a simple getSize() on the array will give me the total number of working days ![;-)]( "Wink")

So finally this is the best approach because with the array you can extract more results. ![:)]( "Smile")

---

<div class="post-metadata">

**Author:** ![Foobrother](https://avatars.discourse-cdn.com/v4/letter/f/50afbb/32.png) [@Foobrother](https://forum.servoy.com/u/Foobrother)\
**Post date:** [September 11, 2009, 3:36pm UTC](https://forum.servoy.com/t/get-working-days-between-2-dates/11136/18 "2009-09-11T15:36:00Z")

</div>

I have tried the updated plugin.

It seems to work. However when you have the same date for start/end I have a problem. It returns 0  
You will tell me that it’s stupid to calculate for one day. But I use this system for holidays management. So people might want to take only 1 day off.

Here is the code I use (I want start/end days to be included):

```auto
var days = plugins.DateUtils.getWorkingDays(new Date(arguments[0]), new Date(arguments[1]), [6,7]);
return (days.length) - 1;

```

---

<div class="post-metadata">

**Author:** ![patrick](https://avatars.discourse-cdn.com/v4/letter/p/b2d939/32.png) [@patrick](https://forum.servoy.com/u/patrick)\
**Post date:** [September 12, 2009, 10:49am UTC](https://forum.servoy.com/t/get-working-days-between-2-dates/11136/19 "2009-09-12T10:49:04Z")

</div>

That was actually the only question. Should the first date given be included or not? So I guess it should. I will change that and send you an update.

---

<div class="post-metadata">

**Author:** ![Foobrother](https://avatars.discourse-cdn.com/v4/letter/f/50afbb/32.png) [@Foobrother](https://forum.servoy.com/u/Foobrother)\
**Post date:** [September 14, 2009, 8:59am UTC](https://forum.servoy.com/t/get-working-days-between-2-dates/11136/20 "2009-09-14T08:59:33Z")

</div>

> patrick:  
> That was actually the only question. Should the first date given be included or not? So I guess it should. I will change that and send you an update.

With your new changes it does seem to count Sunday as a day off. When I do a test with Start on Monday and End on Sunday it says 6 days, if I do from Saturday to Sunday it says 1 day and if I do from Friday to Monday it says 3 days.  
However if I try from Monday to Saturday it says 5 days or Monday to Monday it says 1 day

Here is the code I use:

```auto
	var days = plugins.DateUtils.getWorkingDays(new Date(arguments[0]), new Date(arguments[1]), [6,7]);
	return (days.length);

```

[Next page](https://forum.servoy.com/t/get-working-days-between-2-dates/11136.md?page=2)
