Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

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 = issue.reporter.name
def stDate = issue.getCustomFieldValue(cfend)
def enDate = issue.getCustomFieldValue(cfstart)
def timediff = enDate-stDate


@WithPlugin("is.origo.jira.tempo-plugin")

@PluginModule
TempoWorklogManager tempoWorklogManager
@PluginModule
WorkAttributeService workAttributeService


def changeHistoryManager = ComponentAccessor.getChangeHistoryManager()


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

tempoWorklogManager.create(issue.key, TempoDateTime.now(), "Auto-created worklog for Vacation",
attributes,
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

After 3 years, have you found the solution?

Suggest an answer

Log in or Sign up to answer
TAGS

Community Events

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

Find an event

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

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you