Forums

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

How to return the difference between two days as Months: Days: Hours: Minutes:

Mohamed El Taweel February 14, 2019

Hi,

I am using the following script to return the time difference between two date-time custom fields

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.Issue;
import java.util.Date.*

def customFieldManager = ComponentAccessor.getCustomFieldManager();
def dateFieldObject= customFieldManager.getCustomFieldObject('customfield_10220');

def dateFieldObject2= customFieldManager.getCustomFieldObject('customfield_13605');

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

def dateValue = issue.getCustomFieldValue(dateFieldObject) as Date
def dateValue2 = issue.getCustomFieldValue(dateFieldObject2) as Date
def calculation = (dateValue.getTime() - dateValue2.getTime())

  return calculation ;

}

 

How to return the difference between two days as   Months:     Days:     Hours:      Minutes:

 

Thanks

2 answers

0 votes
David Fischer
Community Champion
February 16, 2019

Just a side note: you could also use an app like JMCF to create a calculated _duration_ field, which would not only display the value like a duration but also make it searchable like a duration. 

0 votes
Mohamed El Taweel February 15, 2019

I modified the script as below,

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.Issue;
import  java.util.Date.*


def customFieldManager = ComponentAccessor.getCustomFieldManager();
def dateFieldObject= customFieldManager.getCustomFieldObject('customfield_10802');

def dateFieldObject2= customFieldManager.getCustomFieldObject('customfield_10801');

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

   def dateValue = issue.getCustomFieldValue(dateFieldObject) as Date
   def dateValue2 = issue.getCustomFieldValue(dateFieldObject2) as Date
   def calculation = (dateValue.getTime() - dateValue2.getTime())/(1000*60)
    
    //return calculation /1000 / 3600 / 24 ;
        
   def hours = calculation / 60;
   def minutes = (calculation ).doubleValue() % 60;
        
   def days = hours / 24;
   def years = days / 365;
   hours = hours.doubleValue() % 24;
   days = days.doubleValue() % 365;
        
   return ("Days: " + days.floatValue() + "  Hours: " + hours.floatValue() + "  Minutes: " + minutes.floatValue());
  }

I got the result   Days: 46.499306 Hours: 11.983334 Minutes: 59.0 

 

Q- How to round the floating up to 2 decimals only?

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events