Forums

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

Change from "hours" to "Days/Hours/Mins" in a Scripted Field

Christopher Gronde
Contributor
May 23, 2018

I have a created a scripted field which calculates the amount of hours between two dates that are recorded in two other custom fields.  Now I am being asked to display the time in " XX D xx H xx M but I can not figure out how to do that.  I have also been asked if it is possible to have this calculation exclude weekends.  Is this at all possible?  Please help!

 

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_15968');

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

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()) / 3600000
   
   return (calculation as String)
}

1 answer

0 votes
JohnsonHoward
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.
May 23, 2018

Hi Christopher,

This is a tricky one, however I think it could be done by doing something like this:

import java.util.concurrent.TimeUnit


def date1 = new Date()
def date2 = new Date() - 2
def time = date1.getTime() - date2.getTime()
def form = String.format("%d hours, %d min, %d sec",
TimeUnit.MILLISECONDS.toHours(time),
TimeUnit.MILLISECONDS.toMinutes(time),
TimeUnit.MILLISECONDS.toSeconds(time)
);
return form

No entirely sure how you would go about excluding weekends. By that do you mean lets say you 04/01/2018: 00:00:02 which is a Monday and a 01/01/2018: 00:00:01 which is a Friday you exclude the weekend so the time between them is 0 days, 0 hours, 1 min instead of 2 days, 0 hours, 1 min? 

In that case you'd have to find out how many weekend days occurred between the 2 dates, then times 86,400,000 (Milliseconds in a day) by the amount of days and minus that off your total 'time' variable. something like that anyway, might not be completely correct.

Btw the scripted field needs to be a text field.

Thanks

Johnson Howard (Adaptavist)

Suggest an answer

Log in or Sign up to answer