Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Scriptrunner - Find the time difference between 2 date / time fields

Brooke Anglin January 25, 2019

I have the following scripted field. It is returning a value but it isn't the correct value. See code below and example.

 

import com.atlassian.jira.component.ComponentAccessor;

import com.atlassian.jira.issue.Issue;

import  java.util.Date.*  

 

def customFieldManager = ComponentAccessor.getCustomFieldManager();

def dateFieldObject= customFieldManager.getCustomFieldObjectByName('Restored Date/Time');

def dateFieldObject2= customFieldManager.getCustomFieldObjectByName('Incident Start Date/Time');

 

if(issue.getCustomFieldValue(dateFieldObject) && issue.getCustomFieldValue(dateFieldObject2))

{

   def dateValue=  issue.getCustomFieldValue(dateFieldObject) as Date   

   def dateValue2=  issue.getCustomFieldValue(dateFieldObject2) as Date  

   return dateValue.getTime()  - dateValue2.getTime()

}

 

EXAMPLE FROM TOOL: ((note MTRS is my scripted field))

Incident Start Date/Time: 2019-01-25 1:51

Restored Date/Time: 2019-01-25 04:52

MTRS: 17 weeks, 6 days, 16 hours, 40 minutes

 

The correct duration is 3 hours, 1 minute

 

 

Can anyone help me with this please ????

 

2 answers

0 votes
Tarun Sapra
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 25, 2019

Hello @Brooke Anglin

Possible duplicate

https://community.atlassian.com/t5/Jira-questions/ScriptRunner-find-number-of-days-between-two-custom-date-fields/qaq-p/596551

You can add some logging and then see the unit of time returned and then you can convert it to the unit you want and return it as "double"

0 votes
Nic Brough -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.
January 25, 2019

Have a look at the raw number that your subtraction gives you - your scripted field is converting it to text based on the wrong unit of measure.

Brooke Anglin January 25, 2019

the raw number also doesn't make any sense either. Another example is the duration is 20 min but the number template displays 1,200,000 and the duration template displays 1 week, 6 days, 21 hours, 20 minutes 

How can the unit measure be corrected?

Tarun Sapra
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 25, 2019

Hello @Brooke Anglin

It's correct :)

the number displayed is in milliSecond.

image.png

Brooke Anglin January 25, 2019

WHY DID I NOT THINK OF THAT ?????? I was able to divide by 1000 and it gave me the correct duration 

 

 

THANK YOU THANK YOU THANK YOU

Burak Çetin
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
April 10, 2023

Can you post the final version of the code?

Nic Brough -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.
April 11, 2023

Welcome to the Atlassian Community!

All you need to do is convert the milliseconds result on the last "return" lline into seconds (dive by 1,000) or minutes (divide by 60,000)

Suggest an answer

Log in or Sign up to answer