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
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.