Forums

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

How to do a validation to pick date between current day and custom field date?

Swarna Radha
Contributor
June 20, 2019

Hi,

I want to do a validation which needs to pick date between current day and custom field date (BOM due date).

Please find the script below:

if(issue.dueDate.compareTo(cfValues["BOM Due Date"]) <= 0)
{
return true
}
else
false

 

The issue I am facing is that it is taking past dates. For future dates, the script is working fine.

 

For example:

BOM due date: 21/06/2019

Due date: 21/06/2019

Issue is created

 

BOM due date: 28/6/19

Due date: 30/06/2019

Error message  is displayed

 

BOM due date: 28/6/19

Due date: 4/06/2019

No error message is displayed. The validation should work as it should not allow to pick past date.

 

Please advice.

 

Thanks,

Swarna

 

 

1 answer

0 votes
Martin Bayer [MoroSystems, s.r.o.]
Community Champion
June 20, 2019

Can you specify your question better? You're talking about "current date" but it is not mentioned in your condition.

So if you're validationg due date, condition should be for both cases:

  • BOM due date is lower(or equal) to current date AND due date should be in range <BOM duedate, current date>
    • if(issue.dueDate.compareTo(cfValues["BOM Due Date"]) >= 0 && issue.dueDate.compareTo(new Date()) <= 0){ 
      return true;
      }
  • current date is lower(or equal) to BOM Due Date AND due date should be in range <current date, BOM due date>
    • if(issue.dueDate.compareTo(new Date()) >= 0 && issue.dueDate.compareTo(cfValues["BOM Due Date"]) <= 0){
      return true;
      }
Swarna Radha
Contributor
June 20, 2019

Hi Martin,

Thanks for the script above.

When I test for same dateScreenshot_1.png, error message is displayed. Please see screenshot.

Martin Bayer [MoroSystems, s.r.o.]
Community Champion
June 21, 2019

I think the problem is that you're using Date field which does not save time part of the date (which is correct) but new Date() creates date with current time. So instead of new Date() try to create Date instance with one of the methods mentioned here:

https://www.baeldung.com/java-date-without-time

For example:

public static Date getDateWithoutTimeUsingCalendar() {    
Calendar calendar = Calendar.getInstance();
    
calendar.set(Calendar.HOUR_OF_DAY, 0);    
    
calendar.set(Calendar.MINUTE, 0);    
    
calendar.set(Calendar.SECOND, 0);    
calendar.set(Calendar.MILLISECOND,
0);     

    
return calendar.getTime();
}

Suggest an answer

Log in or Sign up to answer