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.

 

Abyakta

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?

Thanks! 

Aidan

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
}

issue.setCustomFieldValue(days,duration)
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,

Aidan

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published Thursday in Jira

5 ways you can make the most of Jira Software and Bitbucket Cloud

As part of the Bitbucket product team I'm always interested in better understanding what kind of impact the use of our tools have on the way you work. In a recent study we conducted of software devel...

73 views 0 5
Read article

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