Forums

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

I created a Scripted field "Duration" to fetch the difference between startdate and duedate

mummareddy supriya
Contributor
March 13, 2025

I can eliminate weekends; but, after excluding weekends, the week must be counted as 5 days.However, according to my script, weeks are equal to 7 days. For example, 10-3-25 to 14-3-25 should count as a week, however it is counting as 5 days.If I provide the dates 10th - 21st, it should count as two weeks, but it only counts as one week three days.Please find the script below.If anyone knows the logic, please let me know.

 

 

 

import com.atlassian.jira.component.ComponentAccessor import java.util.Calendar // Get the issue type def issueType = issue.getIssueType().getName() // Only proceed if the issue is a Story or Task if (!(issueType in ["Story", "Task"])) { return null } // Get the issue's start date and due date def startDate = issue.getCustomFieldValue(ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Start date")) def dueDate = issue.getDueDate() // Ensure both fields have values if (!startDate || !dueDate) { return null } // Function to calculate working days (excluding weekends) int calculateWorkingDays(Date start, Date end) { def calendar = Calendar.getInstance() calendar.setTime(start) int workingDays = 0 while (calendar.time.before(end) || calendar.time.equals(end)) { int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK) // ? Only count weekdays (Monday to Friday) if (dayOfWeek != Calendar.SATURDAY && dayOfWeek != Calendar.SUNDAY) { workingDays++ } // Move to the next day calendar.add(Calendar.DAY_OF_MONTH, 1) } return workingDays } // ? Calculate total working days int workingDays = calculateWorkingDays(startDate, dueDate) // ? Convert to weeks (5 working days = 1 week) int weeks = (workingDays / 5) as Integer int days = workingDays % 5 // ? Format as "X weeks Y days" def formattedDuration = "${weeks} week(s) ${days} day(s)" log.info("Calculated Duration: $formattedDuration") // ? Return value as a long (in seconds) for scripted field return (workingDays * 86400L)

1 answer

2 votes
Radek Dostál
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.
March 14, 2025

Use code blocks.

You're missing line breaks.

You're using arcane groovy magic that does who knows what typing, which combined with the two above is meh.

 

Sure, I can copy paste this to an IDE and work out what's supposed to be a comment and what the code is, but I'm not asking the question.

 

I'm assuming the problem is that you're using a Duration template as an output for the script field, which will work with whatever values you pass to it (i.e. seconds), but the duration formatting will then use Jira's time tracking setting which has defined that 7 days = 1 week, and that's why're seeing a difference between what you do in 

log.info("Calculated Duration: $formattedDuration")

and what you see as a result (or on issue view). Is that assessment so far correct?

 

The duration template should adhere to what you see in `/secure/admin/TimeTrackingAdmin!default.jspa` and thus if you want to use different hours per day per week, you can't use the duration template -- assuming you don't want to change the time tracking settings globally (which would also be pretty confusing for everyone involved as time tracking would suddenly jump a few days/weeks on issues).

 

Then again, using a custom "non out of box Duration" template does have other downsides, such as where it shows up on issue view and how it's searchable (then again script fields shouldn't be searched for anyway). Either way, I think it's due to the global time tracking setting that the Duration script field template adheres to.

 

 

 

 

Suggest an answer

Log in or Sign up to answer