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
}
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:-
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.
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.
I hope this helps to solve your question. :-)
Thank you and Kind regards,
Ram
@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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.