Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,298,482
Community Members
 
Community Events
165
Community Groups

How calculate number of days between created date a custom date field?

How can I get the number of days between two date fields and add them to a custom field? For example, I have a custom date field "Issue Reported Date" and I will create another custom field "Number of days". I would like to get the number of days between the system field "created" and the custom field "Issue Reported Date". The Issue Report Date is always earlier than the issue-created date.  For example, the Reported date is 03/20/2022, and the Issue was created on 03/24/2022.  Here is another logic:

Issue Reported Date: 03/20/2022
Created: 03/24/2022
Number of days: 4

I would like to have the Number of days custom field on the view screen. I searched in the community and saw some posts but I couldn't understand how it works, not sure it can be a Behaviours script, listener, or something else.

I'll be thankful if someone can help me with this.

1 answer

1 accepted

Hello @Shah Baloch ,

Depending on your requirements, it can be Behaviors, post-functions, listeners or Script Fields.

Below is an example using Script Fields with a Number Field template:-

import com.atlassian.jira.component.ComponentAccessor

def customFieldManager = ComponentAccessor.getCustomFieldManager()

def createdDateValue = issue.getCreated()
def issueReportedDate = customFieldManager.getCustomFieldObject("customfield_10800")

def issueReportedDateValue = issue.getCustomFieldValue(issueReportedDate)

if (issueReportedDateValue) {
def daysDiff = createdDateValue - issueReportedDateValue

return daysDiff
}

The output is as below:-

ss1.png

Hope this helps.

Regards.

@Adrien Low _ServiceRocket_thank you for your response. Looks like something is missing, it's showing an error. How can I fix it? Thank you for your help

ScriptRunner_03252022.jpg

@Adrien Low _ServiceRocket_ Sorry to bother you.

The current script is about the Reported date and created date. Is there a way to include the resolution date? I mean if the issue is open it should work like this as you this script. Like if the issue is not resolved it should calculate the date between the Reported date and created date but if the issue gets closed it should calculate the value between "Reported Date" and Resolved Date". Is that possible with a single script and field?

Here is the use case

If resolution = unresolved
Reported Date - Created Date

If resolution = resolved
Reported date - Resolved Date

Thank you for your help.

Hi Adrien, I was able to make it work. Whenever an issue gets created and it'll calculate the reported date and created date, once the issue gets resolved then it changes the value by calculating the reported date and resolved date.

However, I get an error if the reported field is empty. here is the error

java.lang.NullPointerException at org.apache.groovy.dateutil.extensions.DateUtilExtensions.minus(DateUtilExtensions.java:504) at Script163.run(Script163.groovy:19)

This is what I can see from the log:

2022-03-28 14:31:32,691 ERROR [customfield.GroovyCustomField]: ************************************************************************************* 2022-03-28 14:31:32,691 ERROR [customfield.GroovyCustomField]: Script field failed on issue: TP-36, field: Unused java.lang.NullPointerException at org.apache.groovy.dateutil.extensions.DateUtilExtensions.minus(DateUtilExtensions.java:504) at Script163.run(Script163.groovy:19)

Any idea how can I make it work with an empty Reported date? Here is the code that works if the reported date field has a value.

import com.atlassian.jira.component.ComponentAccessor

def customFieldManager = ComponentAccessor.getCustomFieldManager()

def createdDateValue = issue.getCreated()
def resolutionDate = issue.getResolutionDate()

def issueReportedDate = customFieldManager.getCustomFieldObject("customfield_xxxxx")

def issueReportedDateValue = issue.getCustomFieldValue(issueReportedDate)

if (null !=resolutionDate) {

def daysDiff = resolutionDate - issueReportedDateValue

return daysDiff
}
else {
def daysDiff = createdDateValue - issueReportedDateValue
return daysDiff
}

Thank you

Hi @Adrien Low _ServiceRocket_ just wanted to update you that I was able to make it work with an empty date in the reported field. I added a while loop. I don't know it's the best practice but it is working for me. You're welcome to add or remove anything from the code if it's not needed.

Here is the working code:

import com.atlassian.jira.component.ComponentAccessor

def customFieldManager = ComponentAccessor.getCustomFieldManager()

def createdDateValue = issue.getCreated()
def resolutionDate = issue.getResolutionDate()

def issueReportedDate = customFieldManager.getCustomFieldObject("customfield_xxxxx")

def issueReportedDateValue = issue.getCustomFieldValue(issueReportedDate)

while (true)
if (issueReportedDateValue == null) {
break;
}

else {

if (null !=resolutionDate) {

def daysDiff = resolutionDate - issueReportedDateValue

return daysDiff
}
else {
def daysDiff = createdDateValue - issueReportedDateValue
return daysDiff
} }

 

However, I'm still unable to get rid of the "Static type checking" error mentioned in the first screenshot.

I really appreciate your help, without your initial code, I couldn't be able to make it work. Thank you so much!

Hello @Shah Baloch ,

Glad you managed to work out your issues.

Perhaps you can try the below code instead of using a while loop:-

import com.atlassian.jira.component.ComponentAccessor

def customFieldManager = ComponentAccessor.getCustomFieldManager()

def createdDateValue = issue.getCreated()
def resolutionDate = issue.getResolutionDate()

def issueReportedDate = customFieldManager.getCustomFieldObject("customfield_xxxxx")

def issueReportedDateValue = issue.getCustomFieldValue(issueReportedDate)


if (issueReportedDateValue != null) {

    if (resolutionDate != null) {

        def daysDiff = resolutionDate - issueReportedDateValue

        return daysDiff
    }
    else {
        def daysDiff = createdDateValue - issueReportedDateValue
        return daysDiff
    }

}

return null

As for the static type error, you should be able to ignore it as highlighted in the KB below:-

Regards.

Like Shah Baloch likes this

Thank you @Adrien Low _ServiceRocket_ I showed this Issue age field to our team they liked it and they think it's very useful. For another project, we would like to calculate the issue-created date and current date. I'm unable to define the current date. How can I define the current date that I can use to do calculations between the issue created date and the current date?  Thank you for your help.

Hi @Adrien Low _ServiceRocket_ I was able to define the current date

def currentDate = new Date()

One last question, hopefully, it'll be the last one related to this. How can add multiple scripts in a single field? We would like to use this issue age field in multiple projects but all projects do not have a reported date field. For example, in this project we use the Reported date field, the issue created date and resolved date. However, for another project, we would like to use issue created date-current date and created date - resolved date. I just wanted to check if there is a way to use the same field with a different script instead of creating a new field.

Thank you for your help.

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Confluence

An update on Confluence Cloud customer feedback – June 2022

Hi everyone, We’re always looking at how to improve Confluence and customer feedback plays an important role in making sure we're investing in the areas that will bring the most value to the most c...

188 views 1 3
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you