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

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,554,033
Community Members
 
Community Events
184
Community Groups

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

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.
Jan 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-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
Jan 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.

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.
Jan 25, 2019

Hello @Brooke Anglin

It's correct :)

the number displayed is in milliSecond.

image.png

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

Can you post the final version of the code?

Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
Apr 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