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:
But I want the Time and the DateTime like that shown in this online converter:
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!
Solved! Go to Solution.
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")
This saves us from needing to divide by 86400, and adding Date(1970,1,1).
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
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.
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!
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")
This saves us from needing to divide by 86400, and adding Date(1970,1,1).
Hi @seadude,
As an alternative , I made a test.
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
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!
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?
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.