ScriptRunner: Pull Two Custom Date fields and subtract their dates to get the time Difference

Hi Everyone,

I am wondering if there is a way to pull two custom date fields and then subtract them to find the time difference inbetween these two date fields. I am try to use this to be able to log this time automatically inside of Tempo. I imported a lot of plugins just to make sure that I am not missing out on any functions. My code so far can be seen at the bellow:

import java.util.*
import com.atlassian.jira.component.ComponentAccessor;
import com.onresolve.scriptrunner.runner.ScriptRunnerImpl;
import com.onresolve.scriptrunner.runner.customisers.PluginModule;
import com.onresolve.scriptrunner.runner.customisers.WithPlugin;
import com.tempoplugin.core.workattribute.api.WorkAttributeService;
import is.origo.jira.plugin.common.TempoWorklogManager;
import com.tempoplugin.core.datetime.api.TempoDateTime;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.ComponentManager;
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.issue.customfields.manager.OptionsManager
import static java.lang.Math.*
import java.sql.Timestamp
import com.onresolve.jira.groovy.jql.JqlQueryFunction
import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.scriptrunner.runner.ScriptRunnerImpl
import com.onresolve.scriptrunner.runner.customisers.PluginModule
import com.onresolve.scriptrunner.runner.customisers.WithPlugin
import is.origo.jira.plugin.common.TempoWorklogManager
import com.tempoplugin.core.datetime.api.TempoDateTime

def componentManager = ComponentManager.instance
def optionsManager = ComponentManager.getComponentInstanceOfType(OptionsManager.class)
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def cfend = customFieldManager.getCustomFieldObjectByName("End Date")
def cfstart = customFieldManager.getCustomFieldObjectByName("Start Date")
def reporter =
def stDate = issue.getCustomFieldValue(cfend)
def enDate = issue.getCustomFieldValue(cfstart)
def timediff = enDate-stDate


TempoWorklogManager tempoWorklogManager
WorkAttributeService workAttributeService

def changeHistoryManager = ComponentAccessor.getChangeHistoryManager()

def triageType = workAttributeService.getWorkAttributeTypes().returnedValue.find{ == "_WorkType_"}
def attributes = workAttributeService.getWorkAttributesByType(triageType).returnedValue // set the Tempo Worktype attribute

tempoWorklogManager.create(issue.key,, "Auto-created worklog for Vacation",
timediff, 0, null,
issue.reporter, [:])

I know there was a way to log work without tempo (WorklogImp or WorklogImp2) but I had trouble using those functions so I am trying to use the tempoWorklogManager now. I also get issues with the tempoWorklogManager.create command.

1 answer

Update: I changed the below lines

def stDate = issue.getCustomFieldValue(cfend)
def enDate = issue.getCustomFieldValue(cfstart)
def timediff = enDate-stDate

To :

double stDate = (issue.getCustomFieldValue(cfend) as Date).getTime()//returns milliseconds
double enDate = (issue.getCustomFieldValue(cfstart) as Date).getTime()//returns milliseconds

double timediff = (enDate - stDate)*1000 // returns seconds
timediff = timediff/(60*60) // returns hours

This hopefully is able to take my dates, conert them to double and find the time difference inbetween them. 

I am still trying to find a way to log the time difference in either the tempo timesheets or JIRA

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Nov 27, 2018 in Portfolio for Jira

Introducing a new planning experience in Portfolio for Jira (Server/DC)

In the past, Portfolio for Jira required a high degree of detail–foresight that was unrealistic for many businesses to   have–in   order to produce a reliable long-term roadmap. We're tur...

2,698 views 18 21
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