Forums

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

How to disable previous passed dates when picking a date ?

Kou Ssi
Contributor
June 17, 2023

I have a Date custom field in which the user can pick the date he wantes , however the problem is that he can choose a date that's already passed ( for example we are at 17/06 but he can choose the date 15/06 ) how can i disable that for the user and allow him to only chose starting from the current date .

2 answers

2 accepted

2 votes
Answer accepted
Anna Hryhoruk _Appfire_
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 7, 2023

Hello @Kou Ssi ,

Indeed, this can be done using Power Scripts and SIL Validator. Here is an example of such a script:

string format = "dd-MM-yyyy ";
string errorMsg = "You cannot select date in the past!";
date selected_date=getInput("customfield_18400");

if(formatDate(selected_date, format) < formatDate(currentDate(), format)) {
return false, "customfield_18400", errorMsg;
}

You need to replace  customfield_18400 with your custom field id. Also, to allow users to select a date that is the current day, I had to add extra formatting, because by default if you select today's date in the Jira Date Picker field, the output behind will contain also hours and minutes like 2023-07-07 00:00:00, and currentDate() output will be something like 2023-07-07 11:08:41, so it will also compare time part and will not allow you to select current date. 
So if you select the date in the past, you`ll get the error message near the field:
2023-07-07 11 10 56.png

And on top of the page:

2023-07-07 11 11 08.png

Hope this helps!
Anna

Kou Ssi
Contributor
July 18, 2023

Thanks @Anna Hryhoruk _Appfire_ that answer came in handy ! 

Like Anna Hryhoruk _Appfire_ likes this
1 vote
Answer accepted
Evgenii
Community Champion
June 17, 2023

Hi, @Kou Ssi 

If you have ScriptRunner, and you want to limit date, while issue is creating, you can use Validator, or Behaviour with groovy script, that checks, that date is larger then current moment.

Something like this, for Validator:

Date now = new Date()

return issue.created > now.toTimestamp()
Kou Ssi
Contributor
June 17, 2023

Hi, @Evgenii 
Thank you for your response 
in my case i am using power script SIL instead of ScriptRunner

Evgenii
Community Champion
June 17, 2023

As far, as I know, SIL has Validators too. I ant very good familiar with it, but I think, you can check, that date entered is larger than now with next code:

return getInput("customfield_10001") > currentDate()

Found methods here:

https://anovaapps.atlassian.net/wiki/spaces/PSJ/pages/1131021259/getInput

https://anovaapps.atlassian.net/wiki/spaces/PSJ/pages/1129775385/currentDate

Suggest an answer

Log in or Sign up to answer