Smart Values String type conversion to Date

Jason G
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
August 13, 2022

How can I convert a correctly formated (yyyy-MM-dd) string into a Date so that Date and Time operations like minusDays(1).toBusinessDayBackwards behave as expected and return a result.

When smartYear = 2022 & smartMonth = 01 & smartDay = 19

smartParsedDate = {{smartYear.concat("-").concat(smartMonth).concat("-").concat(smartDay).toDate().as("yyyy-MM-dd")}}

 

Results in {{smartParsedDate}} equaling "2022-01-19"

as per https://community.atlassian.com/t5/Jira-questions/Parse-a-datestring-in-a-smartvalue/qaq-p/1879743

however {{smartParsedDate.minusDays(1)}} returns an empty string as does

{{smartParsedDate.toBusinessDayBackwards}}

1 answer

1 accepted

2 votes
Answer accepted
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.
August 14, 2022

Hi @Jason G 

Please try converting the text string to a date value first, such as with:

{{smartParsedDate.toDate}}

Kind regards,
Bill

Mike.Mikaelian December 1, 2023

I ran into this because my expectation was that I only had to do this once, such as when defining a variable. For example, if I create a variable called {{endOfYear}} as follows:

{{now.format("yyyy").concat("-12-31").toDate}}

and try to use it elsewhere, the result depends on how I'm using it. If I just want to print the variable, it works fine:

2023-12-31T00:00:00.0+0000

However, if I want to use it in a calculation, it fails:

{{endOfYear}} is {{now.diff(endOfYear).days}} days before the end of the year 

This produces:

2023-12-31T00:00:00.0+0000 is days before the end of the year

In order to get it to produce the expected result, you have to add the ".toDate" function to the variable where it's being used as a date; the function is lost if you try to make it part of the variable. I was able to make the above smart values work by adding the function to the variable when used in the diff:

{{endOfYear}} is {{now.diff(endOfYear.toDate).days}} days before the end of the year

Produces the result:

2023-12-31T00:00:00.0+0000 is 29 days before the end of the year

I'm posting this here in case anyone else is confused by the answer above. While it's correct, this nuance made it difficult for me to accomplish exactly what the originally poster was trying to do, and the answer above did not provide enough information.

Like Roy Presley likes this
Roy Presley December 9, 2024

I wish I had discovered Mike's clarification sooner. I struggled for several days with smart values I incorrectly thought were "saving" a text string as a Date object.

"In order to get it to produce the expected result, you have to add the ".toDate" function to the variable where it's being used as a date; the function is lost if you try to make it part of the variable."

After following Mike's suggestion, I was finally able to get the toDate modifier working with a smart value. Based on this my impression is smart values are actually text strings which can only be used as Date objects in place with one of the date modifiers.

Like # people like 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.
December 9, 2024

Hi @Roy Presley -- Welcome to the Atlassian Community!

For Jira Cloud, created variables have always stored their values as text: https://support.atlassian.com/cloud-automation/docs/jira-automation-actions/#Create-variable

And so they must be converted when used as other types: date, number, list, etc.  The method of using the "source" smart value expression directly to preserve the typing works in some cases...but not in others.  For example, where the smart value is dynamically looked up, requiring it to be first stored in a variable and converted later.

 

For Jira Data Center, there appears to be undocumented behavior for created variables.  That is, they are not always text values...even when the "source" expression was text!  Thus the workaround is often two-fold:

  • first convert to text, such as with .trim() or .text
  • and then convert to the needed type, such as with toDate

 

Kind regards,
Bill

Like Jason M. likes this
Roy Presley December 9, 2024

Bill,

Thanks for the clarification and work-around for Data Center's undocumented behavior. I will definitely save this one for future use!

Like Bill Sheboy likes this

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
PREMIUM
TAGS
AUG Leaders

Atlassian Community Events