Smart Values String type conversion to Date

Jason G 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

1 vote
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
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!
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.

Suggest an answer

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

Atlassian Community Events