Forums

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

Script runner: Need script to find the difference months

Mark Hitt June 28, 2023

Hello,

I'm looking for a script that will calculate the difference in months between two custom date fields and enter that difference into a third scripted field. For example

CustomField1 = 1/1/23

CustomField2 = 3/1/23

ScriptedField = 2 

 

I found this script that calculates the difference in days, but I'm looking for months.  

 

import com.atlassian.jira.component.ComponentAccessor;

import com.atlassian.jira.issue.Issue;

import  java.util.Date.*

def customFieldManager = ComponentAccessor.getCustomFieldManager();

def dateFieldObject= customFieldManager.getCustomFieldObjectByName('CustomField1');

def dateFieldObject2= customFieldManager.getCustomFieldObjectByName('CustomField2');

if(issue.getCustomFieldValue(dateFieldObject) && issue.getCustomFieldValue(dateFieldObject2))

{

   def dateValue=  issue.getCustomFieldValue(dateFieldObject) as Date    

   def dateValue2=  issue.getCustomFieldValue(dateFieldObject2) as Date    

   return dateValue - dateValue2
}
Thank you

 

1 answer

1 accepted

0 votes
Answer accepted
Ram Kumar Aravindakshan _Adaptavist_
Community Champion
July 1, 2023

Hi @Mark Hitt

For your requirement, I suggest trying the working code below:-

import com.adaptavist.hapi.jira.issues.Issues
import java.text.SimpleDateFormat

def currentIssue = Issues.getByKey(issue.key)

def date1 = currentIssue.getCustomFieldValue('Date1') as String
def date2 = currentIssue.getCustomFieldValue('Date2') as String
def format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")

def monthsBetween(int fromYear, int fromMonth, int fromDayOfMonth, int toYear, int toMonth, int toDayOfMonth) {
def cfrom = new GregorianCalendar(fromYear, fromMonth, fromDayOfMonth)
def cto = new GregorianCalendar(toYear, toMonth, toDayOfMonth)
((cto.get(Calendar.YEAR) - cfrom.get(Calendar.YEAR)) * cto.getMaximum(Calendar.MONTH)) + (cto.get(Calendar.MONTH) - cfrom.get(Calendar.MONTH))
}

if (date1 && date2) {
def date1Value = format.parse(date1)
def date2Value = format.parse(date2)
def calendar1 = Calendar.instance
def calendar2 = Calendar.instance

calendar1.setTime(date1Value)
calendar2.setTime(date2Value)

monthsBetween(calendar1.get(Calendar.YEAR), calendar1.get(Calendar.MONTH), calendar1.get(Calendar.DAY_OF_MONTH),
calendar2.get(Calendar.YEAR), calendar2.get(Calendar.MONTH), calendar2.get(Calendar.DAY_OF_MONTH))
}

Please note that the working code above is not 100% exact to your environment. Hence, you will need to make the required modifications. 

Below is a screenshot of the Scripted Field Configuration:-

scripted_field.png

The main calculation is performed by the monthsBetween(...) method. It counts the difference between the two dates in Months.

Below are a couple of test screenshots for your reference:-

1. When the issue is being created, both the date fields must be filled. The Scripted Field will also be empty if either date field is empty. In this example, for Date1, the date has been set to 1st January 2023; for Date2, the date has been set to 1st July 2023.

test1.png

2. Once the ticket has been created, as expected, the Scripted Field is displayed with the difference between the two dates in Months. The screenshot below shows that the Scripted Field displays the value of 6, i.e., six months difference.

test2.png

I hope this helps to solve your question. :-)

Thank you and Kind regards,
Ram

Mark Hitt July 5, 2023

@Ram Kumar Aravindakshan _Adaptavist_ 

Thank you so much for taking the time to help me out. 

Your script worked and I was able to calculate the difference in months.

 

 

 

 

Again, thanks for taking the time to help me. 

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
VERSION
9.4.7
TAGS
AUG Leaders

Atlassian Community Events