Test difference between 2 dates

Christophe Leite July 4, 2013

Hi there,

Is this possible to evaluate the duration between a specified date on a form of a workflow, and the system date ? that what I want to do, in order to show (if this possible too) a short message if 1 day occurs since the specified date above, forbidding the transition of the status Closed to Reopened...

Thanks a lot,

Christophe

7 answers

1 accepted

2 votes
Answer accepted
Justin Alex Paramanandan
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 8, 2013

Hi Christophe,

As @Renjith mentioned, you can use the Script Runner plugin, plus the following code:

Date now = new Date()
Date cfDate = new Date(cfValues['YourCustomField'].getTime())
new Date(now.getYear(), now.getMonth(), now.getDate()).compareTo(cfDate) <= 0

This is the script that should be placed in the Reopened transition as a validator.

*note: I've also answered you on StackOverflow. :)

Christophe Leite July 8, 2013
Hi Justin, Thanks for your answer. I appreciated it. Above code (java code?) you post is to test if both dates are identifical. But if you want to compare them, allowing only 1 day of difference, and in case where my date (specified in custom field) must be former to system date... my date must be former to system date the number days between this dates must be less or equal to 1 (only 1 day of difference) Great thanks to you, Best regards, Christophe *note : yes, I've posted on StackOverflow, cause currently I cannot progress with my workflow unfortunately...
Justin Alex Paramanandan
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 8, 2013

The above code is a Groovy-based code which works well with the Script Runner plugin.

Yea, back to your question. You mentioned that the above code tests for comparison on whether the dates are identical. That is indeed true; however, since we are placing this in the validator for the workflow, it would return either true or false.

Hence, we are comparing the custom field with the current date (system date). If the custom field is former than the system date (>0), that would invalidate the transition and hence, blocks it.

Here's an example of it being done:

1 vote
Christophe Leite July 9, 2013
Dear Justin, Here is the code which works : Date now = new Date() Date cfDate = new Date(cfValues['YourCustomField'].getTime()) long elapsedtime = now.getTime() - cfDate.getTime() boolean res = elapsedtime <= 86400000 && elapsedtime >= 0 Thank you very much for your help. I appreciated it. Best regards, Christophe
Justin Alex Paramanandan
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 9, 2013

Awww, and I was just writing the code for this! :D

Nevertheless, good work on finding it, man! We can share these two scripts out for all to see! :)

1 vote
Renjith Pillai
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 6, 2013

Yeah, if you can use Script runner. Add a validator and do the test inside that. https://jamieechlin.atlassian.net/wiki/display/GRV/Script+Runner

0 votes
Khanh Nguyen
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.
May 13, 2015

thanks for sharing. . . this is very close to what I need.

however. . . This code

Date enteredTime = new Date(cfValues[customfield_10237].getTime())

and the logs shows this error. . .

groovy.lang.MissingPropertyException: No such property: cfValues for class: Script33

thanks in advance!

0 votes
Christophe Leite July 8, 2013
Here is the code I wrote Date now = new Date() Date cfDate = new Date(cfValues['YourCustomField'].getTime()) new Boolean(((now.getTime() - cfDate) / 86400000) <= 1) && (now.getTime() >= cfDate )) But it doesn't work... the problem is I don't know about java syntax, cause I don't know this programming language :-(
0 votes
Christophe Leite July 8, 2013
Thank you for your answer. It works to allow transition when dates are similar, but my purpose was modified by my responsible. He would like to allow the transition if dates are similar or if the duration between them is only 1 day or less. Example : System date is 09/07/2013 (Paris) My date (dd/mm/yyyy format) Transition allowed Why 07/07/2013 NO my date is former to system date 08/07/2013 NO my date is former to system date 09/07/2013 YES my date and system date equals 10/07/2013 YES 1 day occur between 2 dates 11/07/2013 NO 2 days occur between 2 dates Excuse me for my english. I'm french, and I try to improve my English. Thanks a lot.
0 votes
Christophe Leite July 7, 2013

Maybe I could use dateCompare function ? or the following ... ?

I have to compare the closing date (specified on a form by an user) and system date. To explain, I would like to test :

IF system date - closing date <= 1 day THEN

allow transition from Closed to Reopened status

ELSE

decline transition above, and return an error message to user.

END

Any help greatly appreciated.

Best regards,

Christophe

Renjith Pillai
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, 2013

Did you read my reply above?

Christophe Leite July 7, 2013

Yes, thanks but I did not find a sample for my purpose :-(

I'm still looking for one solution ...

Suggest an answer

Log in or Sign up to answer