Solved

Convert unix timestamp to datetime

  • 21 March 2024
  • 6 replies
  • 31 views

Userlevel 2
Badge +2

Hello,

I hoped somebody has already worked this out so I don’t have to 🤣.

 

I have a Epoch Unix Timestamp value that I want to convert into normal DATETIME format in Ataccama.

e.g. 1711015200

 

I was expecting converted value to be 2024-03-21 10:00:00 (GMT).

But when I do a toDateTime() on it, it gives me 1970-01-20 20:16:55. So I guessed wrong.

I thought about using dateadd() but that one does not support seconds.

 

Any ideas?

icon

Best answer by maykwok_hamilton 25 April 2024, 16:15

View original

6 replies

Userlevel 4
Badge +4

Hi @maykwok_hamilton 

toDateTime expect number of milliseconds, while your value is in seconds. To get expected value you need to multiply it by 1000.

Userlevel 2
Badge +2

Awesome, that worked, thank you!!

Userlevel 2
Badge +2

Hi @AKislyakov ,

 

I’m now hit by daylight savings…!

I need the toDatetime to give me UTC time, but at the moment in the UK it’s giving me British Summer Time.

I see if a datetime is string I can specify locale. But I can’t find what locale, or syntax that is required to achieve this.

Any ideas?

Thanks!

Userlevel 2
Badge +2

My exact problem:

I call an API, it gives me the unix timestamp back, say for example: 1714035600.

This translates to 2024-04-25 09:00:00 UTC, or 2024-04-25 10:00:00 BST.

Because my ONE Desktop observes daylight savings, the toDateTime() expression gives me 2024-04-25 10:00:00.

I store this down into RDM. RDM thinks this is UTC.

So I now have RDM Web UI displaying this as 2024-04-25 11:00:00.

😭

Userlevel 2
Badge +2

A kind soul has shared with me a formula that does exactly what I need:

 

dateAdd(now(), -1 * toInteger(toString(now(), "Z")) div 100, "HOUR")

 

(Of course, Replace “now()” with whatever datetime value you have)

Userlevel 6
Badge +7

Thank you for sharing it here @maykwok_hamilton 🙌

Reply