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)