It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Scriptrunner Cloud Comparing A Custom Date/Time Value to Now in Minutes

I have several custom date/time fields that return a value following the Jira standard format; 

"yyyy-MM-dd'T'HH:mm:ss.SSSZ" 

2020-02-01T22:34:00.000-0500

I'm trying to compare a custom date/time field against "now", to yield a result in minutes. Then I can say "if result is less than x minutes, do something. I'm comfortable with the if/else statement. Where I could use help is correctly formatting the date/time as a math problem (desired value = custom field - now).

Am planning on getting the desired value from an issueKey via a query via schedule job, rather than a listener, however even if I change to use a listener, still seem to be stuck on how to subtract time then convert into minutes (or hours).

I think where I've hit trouble is converting the value into strings, since they can't be compared. I've also tried simply subtracting the future time (variable) from now and re-formatting the difference but that didn't work. I've found this post for cloud code from the Adaptavist library, but is failing to compile "unable to resolve class DateTimeFormatter"

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ")

Seems like a formatting problem and I'm likely over-complicated matters. Reminder, this is for Scriptrunner CLOUD. Anyone familiar with time (duration) comparison? Thanks ahead.

3 answers

1 accepted

0 votes
Answer accepted

Hi Adam

Here is a code snippet (without calling the Java API) that compares the date string you have posted to the current date and gets the difference in minutes. I think this is the snippet you are looking for:

paste this into the script console and execute it:

def pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
def myDateString = "2020-02-01T22:34:00.000-0500"
def myDate = Date.parse(pattern, myDateString)

def currentDate = new Date()
def currentTimeInMS = currentDate.getTime()
def fieldTimeInMS = myDate.getTime()

def differenceInMinutes = (currentTimeInMS - fieldTimeInMS)/1000/60
//log.warn('The difference in minutes is: ' + differenceInMinutes)

 

differenceInMinutes will be negative, if myDate it is in the Future and positive, if it is in the past.

Cheers
Leonard

Fantastic I appreciate the help Leonard, this resolves my issues! I was over-complicating matters. The .getTime() class works for the cloud console. 

And Kristian I will create an account and let your team know that Leonard's post is simply the answer they need. There are way too many similar posts all over the place that made this much more complicated than it needed to be.

Thank you again Leonard!

Kindly,

Adam

Like # people like this

Hi Adam,

Thank you for your question.

I can confirm that the code suggested above will not work for Jira Cloud due to the fact that the code you have provided is foo ScriptRunner for Jira Server and this will not work as Atlassian only provide a rest API in Jira Cloud and do not provide a Java API in the cloud-like they do in Jira Server.

Also, I can confirm that the community answer located here explains how to compare two date-time fields inside of ScriptRunner for Jira Cloud and may be a useful reference to help you to achieve your requirements.

I can also confirm that as the page above mentions, to convert the string to a date/timestamp that you will need to use some of the standard groovy methods for parsing dates to do this and I can confirm the page here explains how to do this in more detail

I hope this helps.

Regards,

Kristian

Thanks for the correction, @Kristian_Walker__Adaptavist_ .

I've noticed that ScriptRunner for Jira Cloud is VERY different to the server version. 
I now know I should not answer questions concerning Scriptrunner on Jira Cloud :-).

Cheers Leonard

Hi Leonard,

I can confirm that you can see a detailed explanation of the differences between the server and cloud versions located here in case this helps.

Regards,

Kristian

Like Leonard Chew likes this

@Kristian_Walker__Adaptavist_  as always thanks for your quick response. I'm a little confused, let me describe why and hopefully can be straightened out:

The one line of code I pasted, is a copy from an Adaptavist blog for Cloud. If that code is incorrectly referencing server, can you please speak with your team about removing the page or pointing it correctly?

The field value is set to a string in the Scriptrunner custom date field example. Perhaps I'm asking for help with Groovy context in date/time formatting more than a Scriptrunner specific question, but my understanding is strings cannot be compared. Once they are converted, they'd need to be converted back to yield a duration in minutes as a comparison - which I'm thinking is tripping me up. Suppose what I'm wondering is if its possible to compare datevalue against datevalue without the need for a 3rd custom field. 

Hi Adam,

My apologies when I mentioned server code, I meant the code pasted into this thread by Leonard.

If you are having an issue with the script in the Script Libray then I would advise raising a request in Script Libray Support Portal located here.

As for your other question to compare date values you would need to convert the dates to the correct format and store these in variables inside of your script so these can be compared.

I hope this helps.

Regards,

Kristian

Hi Kristian,

Okay that makes sense, yeah please disregard Leonard's comment.

Right the last part regarding converting dates to the 'correct format', I believe that is where I'm struggling and was posting looking for help here from the community on this matter. If anyone has modern suggestions I would certainly appreciate the assistance. The posts I came across are at least 5+ years old or reference deprecated/non supported classes.

Also I'm finding the Script Library Support Portal is gated and I'm not sure if I have credentials. 

Kindly,

Adam

Hi Adam,

I have just tested the code posted by Adam at the bottom of this page this morning which compares the date string in your format and can confirm that when run on the Script Console inside of Jira Cloud that this returns the difference between two dates and so would advise looking at this approach.

As for accessing the support portal if you need to raise a request then you will first need to sign up for an account here so that you can then login and raise a request.

I hope this helps.

Regards,

Kristian

Hi Adam

Try this:

You can paste the script into the script console.

Change the key with a valid key of yours and change the custom field to the custom field of yours.

The script will log the difference of your field to now in minutes.

import org.apache.log4j.Level
log.setLevel(Level.DEBUG)

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.Issue;

def issueKey = "TEC-2238" // change with a valid key
def issueManager = ComponentAccessor.getIssueManager()
def issue = issueManager.getIssueObject(issueKey)
def customFieldManager = ComponentAccessor.getCustomFieldManager()

log.debug('my issue summary is: ' +issue.summary)

def startDateField = customFieldManager.getCustomFieldObjectsByName("Start Date")[0] // change to your custom field
def startTime = issue.getCustomFieldValue(startDateField) as Date

def currentDate = new Date()
def currentTimeInMS = currentDate.getTime()
def fieldTimeInMS = startTime.getTime()

def differenceInMinutes = (currentTimeInMS - fieldTimeInMS)/1000/60
log.debug('The difference in minutes is: ' + differenceInMinutes)

A note for readers: This code only works on jira-server and not on jira cloud, as it fetches the customfield data through the java-api.

Suggest an answer

Log in or Sign up to answer
Community showcase
Published in Marketplace Apps & Integrations

How to run PI Planning in Jira with Remote Teams

  There is nothing better than having everyone in the same room for PI, tribe or big room planning. But it’s not easy, it’s expensive and not everyone can travel.  Kendis offers solution,...

196 views 1 7
Read article

Community Events

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

Find an event

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

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you