cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Super User
Super User

Convert Unix Time to Human Readable Time

Hello,

I'm trying to convert Unix Timestamps (example: 1546096196) to Time and DateTime values in PowerApps. I can convert to a Date by dividing the Unix Timestamp by 86400 and adding Date(1970,1,1).

Example:

First(colCurrentWeather.sys).sys.sunrise / 86400 + Date(1970,1,1)

This results in:

image.png

But I want the Time and the DateTime like that shown in this online converter:

image.png

I've tried:

DateTimeValue(
    Text(
        First(colCurrentWeather.sys).sys.sunrise / 86400 + Date(1970,1,1), ShortDateTime))

Does anyone know what I'm missing?

Thank you!

1 ACCEPTED SOLUTION

Accepted Solutions
Super User
Super User

Re: Convert Unix Time to Human Readable Time

Hi @seadude

An interesting bit of behaviour is that PowerApps is based on JavaScript, and JavaScript works usually with datetimes based on the number of milliseconds from the Unix epoch.

Because of this, we can take the Unix time, multiply by 1,000 (eg convert from seconds to milliseconds), and use the regular PowerApps formatting functions. In your example, the formula would look like this.

Text(1546096196000, "[$-en-US]ddd, dd mmm yyyy hh:mm:ss")

image.png
This saves us from needing to divide by 86400, and adding Date(1970,1,1).

View solution in original post

8 REPLIES 8
johnsonjohn
Level: Powered On

Re: Convert Unix Time to Human Readable Time

Hello,

 

Your expression is fine.

If you are using a text field or label, try using a 'datepicker' instead.

Put the default value of date picker to your expression and in the advanced properties of date picker choose DateTimeFormat.LongDateTime as Format

Super User
Super User

Re: Convert Unix Time to Human Readable Time

Hi @johnsonjohn. Thank you for the idea. It looks like this defaults to 00:00 and not the actual time encoded in the Unix Timestamp.

image.png

I was able to do this with a label as well using the Text() function along with .LongDateTime. But again, it shows as 00:00 rather than the actual time in the Unix Timestamp.

Any other ideas?

Thanks!

Super User
Super User

Re: Convert Unix Time to Human Readable Time

Hi @seadude

An interesting bit of behaviour is that PowerApps is based on JavaScript, and JavaScript works usually with datetimes based on the number of milliseconds from the Unix epoch.

Because of this, we can take the Unix time, multiply by 1,000 (eg convert from seconds to milliseconds), and use the regular PowerApps formatting functions. In your example, the formula would look like this.

Text(1546096196000, "[$-en-US]ddd, dd mmm yyyy hh:mm:ss")

image.png
This saves us from needing to divide by 86400, and adding Date(1970,1,1).

View solution in original post

Community Support Team
Community Support Team

Re: Convert Unix Time to Human Readable Time

Hi @seadude,

 

As  an alternative , I  made a test.

21.PNG

 

set Text property of the label to :

Concatenate(Text(DateTimeValue("1970/1/1 1:50:24 "),DateTimeFormat.LongDateTime24),"GMT")

 On your side, you can try to set to :

Concatenate(Text(First(colCurrentWeather.sys).sys.sunrise / 86400 + DateTimeValue("1970/1/1 1:50:24 "),DateTimeFormat.LongDateTime24),"GMT")

Regards,

Eason

Community Support Team _ Eason YU
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Super User
Super User

Re: Convert Unix Time to Human Readable Time

Thank you @timl, this was the easiest to implement. I would have never found this on my own! I think I'll suggest it to the Text Function Docs. Might help others too.

 Text(UnixTime * 1000,"[$-en-US]ddd, dd mmm yyyy hh:mm:ss")

 Happy New Year!

WillPage
Level 10

Re: Convert Unix Time to Human Readable Time

The technique @timl describes is perfect, it works for me, but I also have a requirement to convert a Power Apps date back into a Unix Epoch format. I believe I can hack it together with a calculation using DateDiff, but given the underlying support for the Unix ticks, is there a native (but undocumented) way of doing that?

Super User
Super User

Re: Convert Unix Time to Human Readable Time

Hi @WillPage 

To carry out the reverse conversion, we can simply use the Value function.

For example, this would return the current date/time in Unix Epoch format.

Value(Now())

 If the data type of your input date is text, you'll need to convert it to a data with DateValue before calling Value.

WillPage
Level 10

Re: Convert Unix Time to Human Readable Time

@timl  Awesome, thanks.

Helpful resources

Announcements
New Ranks and Rank Icons in April

'New Ranks and Rank Icons in April

Read the announcement for more information!

Better Together’ Contest Finalists Announced!

'Better Together’ Contest Finalists Announced!

Congrats to the finalists of our ‘Better Together’-themed T-shirt design contest! Click for the top entries.

Power Platform 2019 release wave 2 plan

Power Platform 2019 release wave 2 plan

Features releasing from October 2019 through March 2020

thirdimage

Community Summit North America

Innovate, Collaborate, Grow - The top training and networking event across the globe for Microsoft Business Applications

Top Solution Authors
Top Kudoed Authors
Users online (4,826)