ScriptRunner - find number of days between two custom date fields

Cynthia Parenteau June 12, 2017

Please help. I need to look at two custom date fields and find the number of days between them.  

I've tried multiple ways and times, to get this to work and I've found things on the internet that say this is a 3 line script - others that seem like endless code ?!?!?! What am i missing? Do i need to define more "components" first?  See screen shots below. 

 

Any help much appreciated. image001.pngimage002.png

 

1 answer

1 accepted

3 votes
Answer accepted
Tarun Sapra
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
June 12, 2017

The code you have written won't work, here is a snippet that should get you close to the solution, as you need to first get the custom field object and then use this object to get the value from the issue's date custom field

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.Issue;
import  java.util.Date.*

def customFieldManager = ComponentAccessor.getCustomFieldManager();
def dateFieldObject= customFieldManager.getCustomFieldObject('customfield_11601');

def dateFieldObject2= customFieldManager.getCustomFieldObject('customfield_11602');

if(issue.getCustomFieldValue(dateFieldObject) && issue.getCustomFieldValue(dateFieldObject2)) {
def dateValue=  issue.getCustomFieldValue(dateFieldObject) as Date
def dateValue2=  issue.getCustomFieldValue(dateFieldObject2) as Date
return dateValue - dateValue2
}

More here for groovy  - https://stackoverflow.com/questions/2755835/duration-between-two-dates-in-groovy 

Tarun Sapra
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
June 12, 2017

A very similar question and answer is here - 

https://community.atlassian.com/t5/Answers-Developer-Questions/Groovy-Scripted-Field-to-compare-dates/qaq-p/540301  (instead of minutes you can easily divide by 48 to change it into number of days)

Cynthia Parenteau June 12, 2017

Tarun!!!!

Thank you!!! It worked (and on the first try!)  Excellent and much appreciated. 

Thanks too for the additional information and links.  I am just getting started with this tool and i'm sure this will be coming in handy!

Best,

Cindy

 

Like Dave Liao likes this
Janardhan July 28, 2017

Hi Tarun,

If we want exclude the weekends what changes need to be done in the script? Could you please guide on this?

 

Thanks,

Jana

Tarun Sapra
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
July 28, 2017
Janardhan July 28, 2017

Hi Tarun, 

thanks for the fast reply, can I use that java code directly becasue I am week in coding :(

 

Thnaks,

Jana

neeta jain September 4, 2020

Hi Tarun,

 

I want to calculate no of weeks from start date to end date.

Your script is giving no. of days between start date and end date.

How can we get no of weeks from it.

 

Regards,

Neeta Jain

Suggest an answer

Log in or Sign up to answer