Parse a datestring in a smartvalue

Seth Hettich December 6, 2021

I'm trying to fill in a date field via automation (smartvalue).  The issue is created from another system via webhook, and I can't change the date format.  How can I have jira parse an arbitrary date format, eg how can I provide the format string to parse the string into a date?

1 answer

0 votes
Fabian Lim
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 6, 2021

Hi @Seth Hettich

You will have to play with the string and date smart value functions.

String smart values: https://support.atlassian.com/cloud-automation/docs/jira-smart-values-text-fields/

Date smart values: https://support.atlassian.com/cloud-automation/docs/jira-smart-values-date-and-time/ 

Regards

Seth Hettich December 6, 2021

I don't see how that would work. The string functions are just things like substring, there is nothing like java.time.LocalDate.parse()

The second link only lets your format a date object in various ways, not generate one from a string (at least not that I can see).

Bill Sheboy
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.
December 7, 2021

Hi @Seth Hettich -- Welcome to the Atlassian Community!

Yes, and...to what Fabian suggests:

Once you get your date into a string/text format, try using toDate to convert it.

Kind regards,
Bill

Seth Hettich December 7, 2021

I can't seem to get it into a string fomat that toDate will accept, eg:

 

"{{issue.description.match("Created Time: (\d\d/\d\d/\d\d\d\d) \d\d:\d\d:\d\d [+]\d\d\d\d").replaceAll("(\d\d)/(\d\d)(\d\d\d\d)", "$2-$1-$3")}}"

 

generates a string the whole date (with slashes)

Bill Sheboy
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.
December 7, 2021

I am unclear what that source format is you are getting, and I recommend starting with just the date in Jira Date format, yyyy-mm-dd, and write the source and the parsed values to the log as you confirm it.  Once that works, try adding the time information.

Seth Hettich December 7, 2021

The input comes in the description from an external system, so I can't change it.

The format is:

 

MM/DD/YYYY HH:MM:SS +Z

Bill Sheboy
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.
December 8, 2021

Thanks for that information.  I recommend parsing out the text version of the date/time as you have done and storing that in a created variable.  Then try the toDate conversion on that.

Seth Hettich December 8, 2021

The issue is that call to replaceAll produces a string of the format "MM/DD/YYYY"

Bill Sheboy
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.
December 8, 2021

In that case, please take your replaceAll result, and save that in a created variable,

Then use other text functions such as substring() to pull that apart and put it into the jiraDate format yyyy-mm-dd, and

Then try the conversion with toDate.

Like Jason M. likes this
Bill Sheboy
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.
March 8, 2024

Hi @Nathan Phillip Brink -- Welcome to the Atlassian Community!

Short answer:

For me it was experimentation and repeatedly reading the docs.  That toDate function can be found here: https://support.atlassian.com/cloud-automation/docs/examples-of-using-smart-values-with-dates/#Converting-text-to-dates

 

Longer answer:

The original Code Barrel blog had lots of info, but that is gone now that the product was acquired by Atlassian.  The current documentation has improved over time, but still has a challenge that many things are not where you would expect them to be, and some of the examples have typos.

Many of the very helpful things are quite well hidden in the examples sections, like these:

Screenshot 2024-03-08 115623.png

 

Screenshot 2024-03-08 115825.png

Plus there are links out to the "based upon..." tools, like regular expressions from Java Pattern class for the match() function.  I recommend jumping out to those when you see them, and then creating a test rule to learn if A4J actually works that way.  Spoiler: the regex appears to be different when used for match().

Kind regards,
Bill

Like Nathan Phillip Brink likes this

Suggest an answer

Log in or Sign up to answer