cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Highlighted
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
Highlighted
Super User III
Super User III

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
Highlighted
Helper I
Helper I

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

Highlighted

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!

Highlighted
Super User III
Super User III

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

Highlighted
Community Support
Community Support

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.
Highlighted

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!

Highlighted
Impactful Individual
Impactful Individual

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?

Highlighted

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.

Highlighted
Impactful Individual
Impactful Individual

@timl  Awesome, thanks.

Helpful resources

Announcements
Community Conference

Power Platform Community Conference

Check out the on demand sessions that are available now!

News & Announcements

Community Blog

Stay up tp date on the latest blogs and activities in the community News & Announcements.

secondImage

Power Platform 2020 release wave 2 plan

Features releasing from October 2020 through March 2021

Community Highlights

Community Highlights

Check out the Power Platform Community Highlights

Top Solution Authors
Top Kudoed Authors
Users online (8,868)