Test difference between 2 dates

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

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. :)

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...

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 votes
Renjith Pillai Community Champion Jul 06, 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

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

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! :)

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 Community Champion Jul 07, 2013

Did you read my reply above?

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

I'm still looking for one solution ...

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.
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 :-(

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!

Suggest an answer

Log in or Join to answer
Community showcase
Sarah Schuster
Posted Jan 29, 2018 in Jira

What are common themes you've seen across successful & failed Jira Software implementations?

Hey everyone! My name is Sarah Schuster, and I'm a Customer Success Manager in Atlassian specializing in Jira Software Cloud. Over the next few weeks I will be posting discussion topics (8 total) to ...

2,789 views 11 18
Join discussion

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot