Just a heads up: On March 24, 2025, starting at 4:30pm CDT / 19:30 UTC, the site will be undergoing scheduled maintenance for a few hours. During this time, the site might be unavailable for a short while. Thanks for your patience.
×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
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.