Forums

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

How to compare 2 custom date fields using script runner Behaviors

Nannette Mori
Contributor
January 16, 2023

Script Runner Version 6.56.0

Jira Version 8.22.6

I am new to script runner and scripting.  Can someone give me an example of how to get the date values of 2 fields and compare the dates and throw an error if the requested date is less than the delivery date.  

1 answer

0 votes
Mohamed Benziane
Community Champion
January 17, 2023

Hi,

I think this script is a good starting point

https://library.adaptavist.com/entity/calculate-the-difference-between-two-dates

when you get the date value you check both of them like this

if(date1 > date2){
instruction code
}
Nannette Mori
Contributor
January 25, 2023

Yes I have that working but the problem that I am having:

def cf1 = getFieldByName(First Date Field)

def cf2 = getFieldByName(2nd Date Field)

def cf1Value = cf1.getValue()

def cf2Value = cf2.getValue()

 

Date DateValue1 = Date.parse('dd/MMM/yy h:m', cf1)

Date DateValue 2 = Date.parse(('dd/MMM/yy h:m', cf2)

if(DateValue1.getTime() < DateValue2.getTime())

{

cf1.setError(Error msg)

}else{

cf1.clearError{}

}

This script works but I am getting the error "groovy Exception, cannot cast object with class 'java.lang.string to class 'java utl.date

So I tried converting my string to a date

However it is not working.

Also in the log when I print out the date field the date format is different than when when I print out the field value....?log.debug - cf1 field is :form field ID: customfile_xxxxx,, value: 24/Jan/23 

when I print out the getvalue  I get th following

log.debug.  The Value of cf1 is Tue Jan 24 13:37:00MST 2023

 

Why are the date formats different between getting the value of the field and printing out the field?

Why is my date conversion not working?

In jira the date displays as 24/Jan/23

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
VERSION
8.22.6
TAGS
AUG Leaders

Atlassian Community Events