Converting string to date object with Automation

Hieu Phan August 9, 2019

I am currently utilizing Automaton for Jira LITE to handle incoming issues from emails. In the issue description, there is a date in a form of a string which I extract with a regex using match(). I can convert the string into a date object using .toDate. However, the converted object is not the correct date.

For example, one of the descriptions has a date of "September 3, 2019". I used .toDate("MMMM d, YYYY") to convert it to a date object with the correct format. I then format that date further to .format("d/MMM/yy"). It should spit out 3/Sep/19. The actual output is 30/Dec/18. When put it into a different format like yyyy-mm-dd, it spits out 2018-00-30. I've also tried changing the month in the description in July, same result. My guess is that my string format may cause Automation to read it incorrectly. 

Could this be related to how I've extracted the string via regex from the description? Should I try formatting the extracted string in a certain way so Jira/Automation can read it correctly? 

1 answer

1 accepted

4 votes
Answer accepted
Nick Menere
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 11, 2019

Hi Hieu,

I think we may need to break this down step by step. Use a log action or add comment with the following:

Parse: {{description.match("regex")}}
Date: {{description.match("regex").toDate}}
Date Format: {{description.match("regex").toDate.as("d/MMM/yy"}}

 

That should hopefully show us where it is breaking.

Cheers,

Nick

Hieu Phan August 12, 2019

Hey Nick,

Thanks for replying.

Here is my output with the regex pulling "July 3, 2019" - 

Parse: July 3, 2019
Date: 
Date Format:

 I believe .toDate requires you to match the format of the string which is why I used .toDate("MMMM d, yyyy") to get an output for that function. Else, it comes up empty as you can see above. 

Like Cat W likes this
Hieu Phan August 12, 2019

I figured out what I did incorrectly - 

 

Seems like there is a difference between YYYY and yyyy when formatting the year for turning a string into a date object. 

.toDate("MMMM d, yyyy") was what I needed to use.

Like Cat W likes this
Jeret Shuck May 13, 2021

@Nick Menere 

I'm effectively trying to do the same as Hieu here. However, when doing so, I'm only getting the following back:

Parse: June

Date:

Date Format:

 

Here's what I have in my automation:

Parse: {{issue.description.match("(January|February|March|April|May|June|July|August|September|October|December) (\d{2}), (\d{4})$")}}

Date: {{issue.description.match("(January|February|March|April|May|June|July|August|September|October|December) (\d{2}), (\d{4})$").ToDate}}

Date Format: {{issue.description.match("(January|February|March|April|May|June|July|August|September|October|December) (\d{2}), (\d{4})$").toDate.as("yyyy/MM/dd")}}
Brittney Hinesly July 11, 2023

Hello @Hieu Phan this has been here a while but I'm hoping you can help me on a similar issue.

I'm currently pulling information from the description field using REGEX due to it being an email request. My goal is to populate the fields from my REGEX pulls.

I'm having trouble getting the "Start Date" date picker field to update with my code.

 

The description displays it as:

Start Date: 7/11/2023

 

My Automation is 

If: matches

First Value: {{issue.description}}

Condition: Contains Regular Expression

Regular Expression: (?<=Start Date:)(\W\d+\\\d+\\\d+)

Then: Edit Issue Fields

Field to Set: Start Date

and this is my smart value:

{{issue.description.match(".*(?<=Start Date:(\W\d+\\\d+\\\d+).toDate("m/dd/yyyy").*”)}}

 

I know it's wrong, I just don't know where or how to correct. If you have any idea please let me know :))

Igrar July 12, 2023

Hi @Brittney Hinesly

What error message you get in audit logs of your automation?

Brittney Hinesly July 12, 2023

Hello 

I've changed it a little bit and finally got it to go through as "Success" in the Audit log but no change to my Start Date field. 

My code is :

If: matches

First Value: {{issue.description}}

Condition: Contains Regular Expression

Regular Expression: (?<=Start Date:\W*)(\d+\/\d+\/\d+)

Then: Edit Issue Fields

Field to Set: Start Date

{{issue.description.match(".*(?<=Start Date:)(\W\d+\/\d+\/\d+).toDate().*")}}

 

when I attempt the last bit of code like this 

{{issue.description.match(".*(?<=Start Date:)(\W\d+\/\d+\/\d+).toDate("MM/dd/yyyy").*")}}

I get an error :

Error rendering smart-values when executing this rule:
Failed to get value for issue.description.match(".*(?<=Start Date:)(\W\d+\/\d+\/\d+).toDate("MM/dd/yyyy")*"): {{issue.description.match(".*(?<=Start Date:)(\W\d+\/\d+\/\d+).toDate("MM/dd/yyyy")*")}}

Brittney Hinesly July 12, 2023

Alright I figured it out. 

If: matches

First Value: {{issue.description}}

Condition: Contains Regular Expression

Regular Expression: (?<=Start Date:\W*)(\d+\/\d+\/\d+)

Then: Edit Issue Fields

Field to Set: Start Date

{{issue.description.match("(?<=Start Date:\W*)(\d+\/\d+\/\d+)").toDate("MM/dd/yyyy").format("yyyy-MM-dd")}}

 

This worked for my Start Date field which is a date picker.

Suggest an answer

Log in or Sign up to answer