Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,299,251
Community Members
 
Community Events
165
Community Groups

How to convert unix epoch timestamp to DateTime

I'm using a Web request to get data from another service.

The Json response look like this.

{
"result": [
  {
  "lastclock": "1601240174",
  "lastvalue": "69.2"
  },
  {
   "lastclock": "1601240174",
   "lastvalue": "50.1"
  }
]
}

lastclock is the timestamp in unix epoch time, and I like to convert this to Jira DateTime object. I tried "toDate" but did not work.

https://support.atlassian.com/jira-software-cloud/docs/use-smart-values-to-manipulate-and-format-dates/

Using the Log Action, the following give no output.

{{webhookResponse.body.result.get(0).lastclock.toDate}}

Maybe I need to provide the time format to "toDate" function?  But I'm not sure what the format should be. 

What would be the best way to convert unix epoch seconds to Jira date time using the Automation for Jira plug-in?

1 answer

Hi @Hongman Kim ,

 

We had the same challenge today and we were able to do a workaround by going back to 1970 and summing the milliseconds to the date you are looking for.

 

First, you create a variable with the 01/01/1970 date

 

1. Create variable

Variable name: epochDate

Smart value: 01/01/1970

 

Then you can use this variable to generate your dates using Epoch, in your case lastclock is returning Seconds, so you should be able to use something like below.

 

2. Log action

{{epochDate.toDate("dd/MM/yyyy").plusSeconds(1601240174)}}

 

In our case we are used Millis so it's 

{{epochDate.toDate("dd/MM/yyyy").plusMillis(1601240174000)}}

 

You can then use this to set JIRA Fields in the Automation :D 

 

Hope this helps!

jira smart values do not allow to use plusSeconds with another smart value though, it seems, so this might be fruitless in most cases... e.g., you can do {{epochDate.toDate("dd/MM/yyyy").plusSeconds(1601240174)}}  but not a dynamic value for 1601240174 within the plusSeconds() (it seems?)

Like Italo _Modus Create_ likes this

@Dane Kantner , we were able to use a dynamic value in the function using a second variable, we used millis but I think this might work with Seconds.

1. Create a variable

Variable name: endDataInMillis

Smart value: {{yourSmartValueHere}}

 

In your action you can use the following:

{{epochDate.toDate("dd/MM/yyyy").plusMillis(endDataInMillis.asNumber)}}

 

It only worked when casting the variable to number using asNumber function.

Like Dane Kantner likes this

thank you so much for the .asNumber, I had been trying to find if there was a need to cast the string->int for this purpose but couldn't find that ... that actually fixed it. In my case I'm actually diffing between a previously opened issue and newly opened and doing some math on that difference, so I'm adding the plusSeconds to the new create date vs 1970... I just needed the smartvariablename.asNumber for it to add the plusSeconds to that create date.

 

The other problem I was having with calculating that date itself was I was trying to use the lookupIssue variable to retrieve the resolutionDate field, and that isn't returned with the lookupIssues smart var. Instead I had to do a lookup for the issue I wanted, and then use a branch to do JQL search of that found lookupIssue, and that does the branch loop on that issue 2 where I can set a smart variable to capture the resolutionDate. Then outside of the branch lookup, I can use that smart var to calculate whatever and set fields in the original issue 1.

Like Italo _Modus Create_ likes this

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Apps & Integrations

Marketplace Partner Spotlight: AppLiger

This month the spotlight is on AppLiger. We caught up with Pavel Pavlovsky, CEO and Product Manager, to learn how the company started and what fuels the team's creativity.    Atlassian:...

389 views 0 7
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you