Script runner field for calculating dates

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 vote
Jenna Davis Community Champion Oct 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

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
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published Jul 10, 2018 in Marketplace Apps

If you’re an Atlassian app developer, you’ll want to know about Atlas Camp!

This September 6-7, hundreds of Atlassian App developers will flock to Barcelona Spain to build skills, discover product roadmaps, meet face-to-face with the Atlassian team, and learn how to extend t...

182 views 0 4
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you