Calculate the number of working days between two dates
Abstract
This code snippet calculates the number of days between two dates excluding the weekends. By default, when you calculate the difference the weekends are included. To exclude them, you can use this snippet.
Logic
- Fetch the dates
- Run a loop to add a day each until the number of remaining days - Skip if the day is a "Saturday" or a "Sunday"
- After adding the days, skip the end if it is a weekend
- Clear the time and return the date
Snippet
Integer workDaysBetween(Date from, Date to) { if (from == null || to == null) return null; Calendar startCal = Calendar.getInstance(); Calendar endCal = Calendar.getInstance(); startCal.setTime(from); endCal.setTime(to); int numberOfDays = 0; startCal.upto(endCal) { startCal.add(Calendar.DAY_OF_YEAR, 1); if (startCal.get(Calendar.DAY_OF_WEEK) != Calendar.SUNDAY && startCal.get(Calendar.DAY_OF_WEEK) != Calendar.SATURDAY) numberOfDays++; } return numberOfDays; } workDaysBetween(issue.get("date1"),issue.get("date2")) //between two date fields workDaysBetween(issue.get("date1"),new Date()) //between one date fields and now
Placeholders
Placeholder | Description | Example |
---|---|---|
<from > | Date to which the number of days should be added | new Date() |
<nod> | Number of days to add | 22 |
Examples
The output of this code snippet is a Timestamp which you could use in a Groovy script, for example, to:
Calculate and display the Due date plus 5 days, excluding weekends, in a Calculated Date/Time field
Date date(Date from, int nod){ Calendar c1 = GregorianCalendar.getInstance(); c1.setTime(from); int weeks = nod/5; int remDays = nod%5; //Adding whole weeks c1.add(Calendar.WEEK_OF_YEAR, weeks); //Run a loop and check each day to skip a weekend for(int i=1;i<=remDays;i++){ c1.add(Calendar.DAY_OF_MONTH, 1); if (c1.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY) c1.add(Calendar.DAY_OF_MONTH, 1); if (c1.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) c1.add(Calendar.DAY_OF_MONTH, 1); } //Skip ending weekend if (c1.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY) c1.add(Calendar.DAY_OF_MONTH, 1); if (c1.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) c1.add(Calendar.DAY_OF_MONTH, 1); //move to 0:00 c1.clearTime(); return c1.getTime(); } date(issue.get("duedate"),5)
Calculate the time to resolution excluding weekends and display a message to the customer in a Calculated Text field
Date date(Date from, int nod){ Calendar c1 = GregorianCalendar.getInstance(); c1.setTime(from); int weeks = nod/5; int remDays = nod%5; //Adding whole weeks c1.add(Calendar.WEEK_OF_YEAR, weeks); //Run a loop and check each day to skip a weekend for(int i=1;i<=remDays;i++){ c1.add(Calendar.DAY_OF_MONTH, 1); if (c1.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY) c1.add(Calendar.DAY_OF_MONTH, 1); if (c1.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) c1.add(Calendar.DAY_OF_MONTH, 1); } //Skip ending weekend if (c1.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY) c1.add(Calendar.DAY_OF_MONTH, 1); if (c1.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) c1.add(Calendar.DAY_OF_MONTH, 1); //move to 0:00 c1.clearTime(); return c1.getTime(); } "Your issue will be resolved on or before " + date(new Date(),10)
References
Related articles