Need help converting Date passed via WebHook
Asked by Doug Levitt:

Hello,

I have a WebHook configured in Jira to send the Sprint created, updated, started, closed, deleted events to an Automation I created.

The Automation receives the data in a smart value as: {{webhookData}}

An example of data received for the Sprint updated is below:

*** ** * ** ***

{webhookEvent=sprint_updated, oldValue={id=3202, self=<https://xxxx.atlassian.net/rest/agile/1.0/sprint/3202>, state=future, name=Test Sprint 12346, originBoardId=260}, sprint={id=3202, self=<https://xxxx.atlassian.net/rest/agile/1.0/sprint/3202>, state=future, name=Test Sprint 12346, startDate=2021-12-13T07:00:00.000Z, endDate=2021-12-20T07:00:00.000Z, originBoardId=260, goal=}, timestamp=1639243910842}

*** ** * ** ***

As you can see, the value I receive for startDate/endDate is formatted as:

***2021-12-13T07:00:00.000Z***

I am not totally certain why, but I am having difficulties trying to use date functions (see: [here](https://confluence.atlassian.com/automation/working-with-dates-993924630.html) and [here](https://support.atlassian.com/cloud-automation/docs/jira-smart-values-date-and-time/) and [here](https://confluence.atlassian.com/automation/date-and-time-functions-993924864.html) and [here](https://support.atlassian.com/cloud-automation/docs/examples-of-using-smart-values-with-dates/) ) on the resulting smart value -- e.g. {{webhookData.sprint.startDate}}

As I really want to convert this from UTC to PST8PDT. I have tried just above everything.

According to the documentation,

|--------------|----------------------------|
| jiraDateTime | 1979-11-01T06:23:12.0-0500 |

But, what I am receiving is formatted slightly differently (I am receiving a \*.000Z instead of a \*.0-0500). ***Is this causing the problem***? Any ideas what the proper way to handle this is? Should I strip off the characters to the right of the dot "."?

Thanks.

*** ** * ** ***

As a note, what I am doing is creating a "record" of each Sprint in a new Jira Project (called Administrivia). This project includes custom fields for: Sprint Id, Sprint Name, Sprint Start Date, Sprint End Date, Sprint Goal ++plus++ statistics about the sprint (Starting count of PBIs, Starting Sum of Story Points, Ending count of PBIs, Ending Sum of Story Points, Average Cycle Time) - ++plus++ Team Name (which, by standard, is embedded in the Sprint Name), Sprint Number (which, by standard, is embedded in the Sprint Name).

This will allow me to create Dashboard of various slices of data. As an example - What are the Sprint Goals of the Active/Open Sprint across 30 Teams operating in parallel Sprints? What is the trend of Average Cycle Time for Squad 1 over the past 20 Completed Sprints. Etc. I am almost completed, I just need to figure out this last thing (hopefully).


Answers:
author: Doug Levitt
body: Hi [@Mykenna Cepek](/t5/user/viewprofilepage/user-id/3527113) , [@Gareth Cantrell](/t5/user/viewprofilepage/user-id/3178836)

Thanks for tag teaming on the answer. I fully missed the fact that I had to convert the string to a date.

I ended up creating two variables as follows:

sprintStartDate  
{{webhookData.sprint.startDate.replaceAll("T", " ")}}

adjustedSprintStartDate  
{{sprintStartDate.todate("yyyy-MM-dd HH:mm:ss").convertToTimeZone("PST8PDT")}}

I imagine I could have combined this, but I think this is more readable.

As a thought, wouldn't it be nice if there was a way to add comments (at various places) to the "code".


author: Mykenna Cepek
body: [@Gareth Cantrell](/t5/user/viewprofilepage/user-id/3178836) is very close with his answer. But it doesn't take into account the *time* *portion* of the original timestamp string (which I assume is important, since the OP cares about the timezone).

[@Doug Levitt](/t5/user/viewprofilepage/user-id/3570640), something like this might get you closer to what you want:

```
{{doug.toDate("yyyy-MM-dd HH:mm:ss.SSS").format("dd/MM/yyyy")}}
```

This seems to work when the 'doug' variable is "2021-12-13 07:08:09.345Z", although the fractional seconds revert to a single digit.

Inserting a convertToTimezone() before the format() should get you what you want.

Some notes:

* Note that I have replaced the "T" character between the date and time portions of the 'doug' variable value with a space character. That is important (and how to do it is left as an exercise for the reader; [hint](https://support.atlassian.com/cloud-automation/docs/jira-smart-values-text-fields/#replace-String-target--String-replacement-)). I wasn't able to convince the toDate() function (with a format argument) to ignore it.
* Curiously, using toDate() with no format argument has no trouble with the "T" character -- but it drops everything after the "T" from the result.
* I did NOT have success trying to store the result of the toDate() function in a variable, and then using additional date functions on that variable. That smells like a type problem (bug?) to me. That's why the above chains the functions together.
* I'm assuming the 000Z your OP represents fractional seconds rather than a timezone offset. Normally timezone values are 4 digits, sometimes with a nested colon, usually with a prefix +/- sign.
* I did not have luck using the canonical "V" format to properly recognize the "Z" as "+0000", but it looks like that is the result anyway (even if it is ignored as shown above).

Please feel free to post what finally works for you!


author: Gareth Cantrell
body: Hi [@Doug Levitt](/t5/user/viewprofilepage/user-id/3570640)

The dates from the webhook are plain text and will need to be converted to dates before you can perform any date-related functions on them.

This is because the Automation engine is not able to deduce that a JSON field is a date or just plain text.

Using:

```
{{webhookData.sprint.startDate.toDate.convertToTimeZone("PST8PDT")}}
```

should do the trick, or to continue in the vein of your example, create a smart value variable using

```
{{webhookData.sprint.startDate.toDate}}
```

and then perform any further date-related calculations on the smart value.

Regards,

Gareth


author: Charlie Gavey
body: Hi Doug,  

Are you able to share a screenshot of the automation rule you're working with, and what you're seeing in the audit log when trying to use date functions?  

Cheers,  
Charlie

comments: author: Doug Levitt
body: Hi [@Charlie Gavey](/t5/user/viewprofilepage/user-id/4670276)

I usually troubleshoot, by sending stuff to Slack. So, in this case:

a) I created a variable called, Doug and set it to:

{{webhookData.sprint.startDate}}

b) I wrote a bunch of stuff to Slack, per below:

![Slack Example.jpg](https://community.atlassian.com/t5/image/serverpage/image-id/173780iDC44453254AE55A2/image-size/large?v=v2&px=999 "Slack Example.jpg")

Below is what was written to the slack channel:

![Slack Outputjpg.png](https://community.atlassian.com/t5/image/serverpage/image-id/173781i314B0CA1F40C6D5D/image-size/large?v=v2&px=999 "Slack Outputjpg.png")

You'll notice I was trying literally anything (as I couldn't figure out why this wasn't working).

The audit log contained no errors (or relevant messages).

Thanks,

Doug