How to make a custom field to show the ∆ between two dates

Nick H August 12, 2022

Using a JMCF custom field I need to calculate the time past between two date fields with an mm/dd/yyyy hh:mm:ss. I need to do this inside of a groovy script inside the custom field w/o importing anything else in. 

  1. What type of JMCF field should be used?
  2. What should the formula/expression be?

 

2 answers

0 votes
Florian Bonniec
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
August 12, 2022

Hi @Nick H 

This should work

 

Date date1 = issue.get("date1");

Date date2 = issue.get("date2");

if (date1 == null || date2 ==null)

  return null;

return (date1.getTime() - date2.getTime())/1000L;

 I think the field type is Calculated (scripted) Duration

 

Regards

0 votes
David Fischer
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
August 12, 2022

Hi @Nick H ,

The most natural field type to pick is the Calculated Duration field, which will display its value in duration format (like  Jira's estimated time field). 

The formula should return a number of seconds, and you can use the secondsBetween(...) global function:

secondsBetween(issue.get("field1"),issue.get("field2"))
Nick H August 15, 2022

Thanks David, looks like this is promising... I messed up and one of the dates is the system "created" date and the other is a custom field that is just dd/mm/yyyy... is there a way to "trim" the system created date to just be dd/mm/yyyy using your formula? Or perhaps a ROUNDUP function similar to an excel formula to put in front of the whole expression? That would work too

David Fischer
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
August 15, 2022

Do you mean the custom field is a Date Picker, not a Date/Time Picker? In that case, are you just interested in a number of days between the two dates? If so, the easiest is to round up the Created date:

import org.apache.commons.lang3.time.DateUtils
def created = DateUtils.truncate(issue.created, java.util.Calendar.DAY_OF_MONTH)
secondsBetween(created,issue.get("field2"))

Suggest an answer

Log in or Sign up to answer