How to convert unix epoch timestamp to DateTime

Hongman Kim
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!
September 27, 2020

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?

2 answers

5 votes
Italo _Modus Create_
Contributor
October 1, 2021

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!

Dane Kantner
Contributor
October 25, 2021

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
Italo _Modus Create_
Contributor
October 26, 2021

@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 # people like this
Dane Kantner
Contributor
October 26, 2021

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
0 votes
Ste404
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 22, 2024

Thanks for this, it almost works but the year is displaying weird for me.

1582853854278 -- 0120-10-27T01:37:34.2+0000

First value is the raw unix timestamp, second is the end result of the comversion.

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

Ste404
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 22, 2024

never mind, I mucked up the year in the smart value.  Works a treat now.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events