Forums

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

Comparing two Date custom field - JMWE - Validator

RIZKY JTUASIKAL
Contributor
June 12, 2023

I have 2 Date custom fields.

Date1 & Date2

 

Example:

Date1 = 06/12/2023

 

How to, 

when submitting status A to B, there will be a Validator,

 

the validator :

Date2 field can only select date from a week (7 days, weekend included) counting from the date selected in the Date1 field.

1 answer

1 accepted

0 votes
Answer accepted
David Fischer
Community Champion
June 12, 2023

Hi @RIZKY JTUASIKAL 

are Date1 and Date2 custom fields of type Date Picker (not Date Time)? And can they be empty or should they both be required?

RIZKY JTUASIKAL
Contributor
June 12, 2023

Hi @David Fischer ,

 

are Date1 and Date2 custom fields of type Date Picker (not Date Time)? And can they be empty or should they both be required?
>> Date Picker type (not Date Time) and they both is required

 

 

Sorry I'm not mention that before..

David Fischer
Community Champion
June 12, 2023

In that case, you can try this:

!!issue.customfield_12345 && !!issue.customfield_67890 && new CalendarDate(issue.customfield_12345).plusDays(7) <= new CalendarDate(issue.customfield_67890)

where  customfield_12345 is Date1 and customfield_67890 is Date2

Like RIZKY JTUASIKAL likes this
RIZKY JTUASIKAL
Contributor
June 12, 2023

Hi @David Fischer ,

 

I was curious,

if Date1 field type is Date Picker, and Date2 field type is Date Picker (Date Time)

 

will the query be the same or different?

David Fischer
Community Champion
June 13, 2023

Hi @RIZKY JTUASIKAL 

it would be different, because you cannot compare Date and CalendarDate objects. So you'd have to convert the Date to a CalendarDate:

!!issue.customfield_12345 && !!issue.customfield_67890 && new CalendarDate(issue.customfield_12345).plusDays(7) <= new Date(issue.customfield_67890).toCalendarDate()

Note that this will convert the date/time value according to the current user's timezone. You can also replace with toCalendarDateUTC() to convert it according to the UTC timezone. 

Don't forget to "Accept" the answer if it works for you.

RIZKY JTUASIKAL
Contributor
June 13, 2023

Hi @David Fischer ,

 

"because you cannot compare Date and CalendarDate objects. So you'd have to convert the Date to a CalendarDate"
>> Does this mean I have to change the Date1 field type to Date Time Picker too?

This is the field I am currently using :

Capture.PNG
Plan CAB Date = Date1 (Date Picker)

Plan Implementation - Date & Time = Date2 (Date Time Picker)

 

 

 

Because, when I tried the query you provided, the validator didn't work.

!!issue.customfield_12345 && !!issue.customfield_67890 && new CalendarDate(issue.customfield_12345).plusDays(7) <= new Date(issue.customfield_67890).toCalendarDate()

Am I missing something?

David Fischer
Community Champion
June 14, 2023

No you don't need to change your custom fields. 

When you say it doesn't work, can you elaborate? Did you try using the "Test Jira Expression" button to test the expression against an issue that has a value for both fields?

RIZKY JTUASIKAL
Contributor
June 14, 2023

here is the result:
result.PNG

 

and here is my query:

!!issue.customfield_12553 && !!issue.customfield_12611 && new CalendarDate(issue.customfield_12553).plusDays(7) <= new Date(issue.customfield_12611).toCalendarDate()

issue.customfield_12553 = Date1

issue.customfield_12611 = Date2

 

 

And here's an example of a date I tested:

Capture.PNG

David Fischer
Community Champion
June 14, 2023

Isn't that what you wanted? 

Unless I misunderstood your initial request and what you want is that Date2 is within a week from Date1 (I thought it must be no earlier than a week after Date1). In that case, try this:

!!issue.customfield_12553 && !!issue.customfield_12611 && new CalendarDate(issue.customfield_12553).plusDays(7) >= new Date(issue.customfield_12611).toCalendarDate()

(you might need to replace >= with > depending on how you want to treat Date2 being 7 days after Date1 e.g. Date2=6/21 when Date1=6/14)

Like RIZKY JTUASIKAL likes this
RIZKY JTUASIKAL
Contributor
June 14, 2023

"Unless I misunderstood your initial request and what you want is that Date2 is withing a week from Date1 (I thought it must be no earlier than a week after Date1)"
>> before that, I  also want to tell you about this hahahaha :')
I'm so sorry David... sorry for my bad english

 

and I've tried your query, and it works really well!!

 

God bless you, @David Fischer 

you made my day

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
STANDARD
PERMISSIONS LEVEL
Product Admin
TAGS
AUG Leaders

Atlassian Community Events