Script runner field for calculating dates

Nick Hizenko October 18, 2017

I'm having hard time with writing script for calculating number of years between 2 dates ( Current date and Custom Datepicker field).

I need to display date difference in years. Date difference between Current date and Custom field.

Im not comfortable with writing scripts, so it would be great if you could provide code for this case.

Thanks a lot 

1 answer

0 votes
Jenna Davis
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.
October 20, 2017

Hello, 

This example should roughly do what you're wanting. I haven't tested it though, so if you have any problems getting it to work let me know.

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

def customFieldManager = ComponentAccessor.getCustomFieldManager();
def dateFieldObject= customFieldManager.getCustomFieldObjectByName('your custom field');

def today = new Date()

if(issue.getCustomFieldValue(dateFieldObject)) {
def dateValue= issue.getCustomFieldValue(dateFieldObject) as Date
def time = dateValue - today // time in milliseconds
return (time/31556952000) // 31556952000 = milliseconds in year
}

Please let me know if you have any questions!

Jenna Davis

Ankit Patel February 1, 2018

How do i modify this script to work in the following way: 

I am looking to find out time in status for current status- 

 

I created a field from a script default fields to detect last time issue changes status - "Status Changed"

 

Then I tired using the above script and modified the custom field, and changed the milliseconds to 3600000 to detect in hours

 

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

def customFieldManager = ComponentAccessor.getCustomFieldManager();
def dateFieldObject= customFieldManager.getCustomFieldObjectByName('Status Changed');

def today = new Date()

if(issue.getCustomFieldValue(dateFieldObject)) {
def dateValue= issue.getCustomFieldValue(dateFieldObject) as Date
def time = dateValue - today // time in milliseconds
return (time/3600000) // 31556952000 = milliseconds in year
}

 

What I get as a result is  -1E-10

can you help me point what am i doing wrong here? 


Thanks

Ankit

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events