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

Abyakta Lenka November 21, 2017

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

2 answers

1 accepted

0 votes
Answer accepted
Aidan Derossett [Adaptavist]
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 21, 2017

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

Abyakta Lenka November 22, 2017

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.

Aidan Derossett [Adaptavist]
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 22, 2017

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

Iram Masood August 14, 2020

this is giving me the error:-

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: Script1.groovy: 1: unable to resolve class com.atlassian.jira.component.ComponentAccessor @ line 1, column 1. import com.atlassian.jira.component.ComponentAccessor ^ 1 error at com.adaptavist.sr.cloud.workflow.AbstractScript.parseScript(AbstractScript.groovy:56) at com.adaptavist.sr.cloud.workflow.AbstractScript.evaluate(AbstractScript.groovy:37) at com.adaptavist.sr.cloud.events.ScriptExecution.run(ScriptExecution.groovy:29) at ConsoleScriptExecution2_groovyProxy.run(Unknown Source)

Iram Masood August 21, 2020

want the similar thing buut for Cloud.Can anyone help here.

0 votes
kranti chavan January 27, 2022

Hi Aidan,

I am using TimeCategory class to get the time difference in my groovy script but it throws error,

groovy.lang.MissingMethodException: No signature of method: org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.use() is applicable for argument types: (java.lang.Class, Script17$_run_closure2) values: [class groovy.time.TimeCategory, Script17$_run_closure2@362b384c]\nPossible solutions: use(java.lang.Class, groovy.lang.Closure), use(java.util.List, groovy.lang.Closure), use([Ljava.lang.Object;), dump(), grep(), any()"}

 

This is my snapshot of script below,

use(TimeCategory) {
def duration = StartDate - CurrentDate
// logger.info("RECEIVED VALUES : days = {}, .hours = {}, minutes = {}, seconds = {}", ${duration.days}, ${duration.hours}, ${duration.minutes},${duration.seconds})
def DaystoHours = (duration.days*24)
def DaystoHourstoMins = (DaystoHours*60)

def HourstoMins = (duration.hours*60)

def SecstoMins = (duration.seconds/60)

def TotalMins = DaystoHourstoMins+HourstoMins+duration.minutes+SecstoMins

WAIT_PERIOD_2 = "PT"+TotalMins+"M"
}

 

Can you plz help me where is an issue?

Suggest an answer

Log in or Sign up to answer