Calculate number of days between startDate (date custom field) and End Date ( date custom field)

I am trying to find number of days between two date custom fields .

Start Date : 02-10-2017

End Date : 04-10-2017

Number of Days should return : 2 days or something similar.

Any help is appreciated.



1 answer

1 accepted

Hey Abyakta!

After a quick google search, it looks like a lot of people enjoy using Groovy's TimeCategory class:

use(groovy.time.TimeCategory) {
    def duration = date1 - date2
    print "Days: ${duration.days}, Hours: ${duration.hours}, etc."

But for your specific scenario could you give me a little bit of context? For example, what are you doing these calculations for and where are you wanting to do them?



for example i have a ticket which marks the start date when the issue is started and when the issue is done marks the end date . i am calculating number of days and ignoring the hours . i am ignoring he number of days its open , number of days its in any status its independent of ticket work day .

i.e : 

Start Date : 02-10-2017

End Date : 04-10-2017 

when i click on done my workflow post function should calculate the number of days and put it in a third custom field called days.

Hey again Abyakta!

You can grab the custom field values and do the arithmetic fairly simply, the only issue you may run into is with setting the custom field value. However, I've written up a post-function that I think should do just about everything that you're looking for. You'll need to fill in some missing details, such as the names of the custom fields; but it should work without much editing. :D

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption

def cfm = ComponentAccessor.customFieldManager
def issueManager = ComponentAccessor.issueManager

def startDate = cfm.getCustomFieldObjectByName("Start Date CF Name")
def endDate = cfm.getCustomFieldObjectByName("End Date CF Name")
def days = cfm.getCustomFieldObjectByName("Days")

def issueStartDate = issue.getCustomFieldValue(startDate) as Date
def issueEndDate = issue.getCustomFieldValue(endDate) as Date

def duration
use(groovy.time.TimeCategory) {
duration = (issueEndDate - issueStartDate).days

issueManager.updateIssue(ComponentAccessor.jiraAuthenticationContext.loggedInUser, issue, EventDispatchOption.DO_NOT_DISPATCH, false)

Give that a look and let me know if it's what you're looking for and if you have any questions about it. Then test it out and see if it works for you (I have not yet tested it personally).

Best of Luck,


Suggest an answer

Log in or Sign up to answer
Community showcase
Posted Sep 18, 2018 in Jira

What modern development practices are at the heart of how your team delivers software?

Hey Community mates! Claire here from the Software Product Marketing team. We all know software development changes rapidly, and it's often tough to keep up. But from our research, we've found the h...

22,405 views 2 7
Join discussion

Atlassian User Groups

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

Find a group

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

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you