Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Groovy - Working Days Calculation (JMCF)

Krzysztof Kiser
Contributor
August 30, 2020

I am using JMCF to create a custom field (calculation of working days between two dates). Here's the script I'm using:

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.user.util.UserManager;
import com.atlassian.jira.issue.MutableIssue
import java.util.Date.*
import java.text.DateFormat;
import java.text.SimpleDateFormat;

DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
def startDate = issue.get("Travel Date From")
def endDate = issue.get("Travel Date To")

Calendar cal1 = Calendar.getInstance();
Calendar cal2 = Calendar.getInstance();
cal1.setTime(startDate);
cal2.setTime(endDate);

def numberOfDays = 0;
while (cal1.before(cal2)) {
if ((Calendar.SATURDAY != cal1.get(Calendar.DAY_OF_WEEK)) && (Calendar.SUNDAY != cal1.get(Calendar.DAY_OF_WEEK))) {
numberOfDays++;
}
cal1.add(Calendar.DATE,1);
}

return (numberOfDays + " Working Days")

 

While the above script calculates the difference correctly for some dates, it shows wrong values for others.

Example 1: 1/Dec/20 - 6/Dec/20 - shows 4 working days - CORRECT

Example 2: 6/Dec/20 - 10/Dec/20 - shows 3 working days - INCORRECT (should be 4 days)


Is there anything wrong with the script? I can't figure out why the calculations are sometimes correct and sometimes not.

I need the values to show me the number of working days between two dates including start date and end date.

Any help would be much appreciated :)

2 answers

1 accepted

0 votes
Answer accepted
Gustavo FĂ©lix
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 30, 2020

Hi @Krzysztof Kiser  That's because the "before" is not including the last day.

In your first example, its not an issue because your end date is on Sunday.

So , I would recommend to add an extra day on the end date

Krzysztof Kiser
Contributor
August 30, 2020

@Gustavo FĂ©lix  thank you for noticing this. I am new to writing this sort of scripts, so I really didn't catch that.

I've amended the endDate to

def endDate = issue.get("Travel Date To")+1

and it now works fine.

Thanks again. Cheers!

Like • Gustavo Félix likes this
0 votes
Radhika Vijji _Innovalog_
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 30, 2020

HI @Krzysztof Kiser 

A much simpler script is available here in JMCF articles: https://innovalog.atlassian.net/wiki/x/kwCLJ

Regards,

Radhika

Aashutosh Kumar
Contributor
May 3, 2021

Hi @Radhika Vijji _Innovalog_ 

Can you please help me,
I want to calculate Working Days in Jira.


https://community.atlassian.com/t5/Jira-Service-Management/Calculate-Working-Day-Business-Day-in-Jira-when-Ticket-is/qaq-p/1682401

For Example: If ticket is 

created  3rd May 2021, i.e. Monday, then Working Day=1
created 11th May 2021, i.e. Tuesday, then Working Day=7


I also need to exclude National/Bank Holidays.

Is this possible any how, using JQL,  Calendar, Groovy Scripts?

I was able to calculate working days, but not exactly how I want using below JQL

created >= startOfMonth() and created <= startOfMonth("+7d") and created >= startOfWeek("+1d") and created <= startOfWeek("+5d")

Suggest an answer

Log in or Sign up to answer
TAGS
atlassian, loom, AI, meeting recording, community

[NEW] Record your meetings with Loom

Welcome to great meetings, with less work. Automatically record, summarize, and share instant recaps of your meetings with Loom AI.

Learn more
AUG Leaders

Atlassian Community Events