Quick Tip: Convert Epoch Time to a Human Readable Date Format

1402
7
05-11-2022 06:54 AM
SeanKMcGinnis
Esri Contributor
2 7 1,402

First, we should understand what epoch time is. Epoch time is also known as Unix time, which is the number of seconds that have passed since 0:00:00 January 1st, 1970. So to get the time/date stamp, we need a way to add these seconds to January 1st, 1970. Lucky for us, Power Automate includes the 'addSeconds' expression that will do the heavy lifting for us for us. Below is an example of using the addSeconds expression.

 

 

addSeconds('1970-1-1', 'Epoch Timestamp', 'MM/dd/yyyy')

 

 

addSeconds - the name of the Power Automate expression doing the conversion

'1970-1-1' - the start of the epoch and what we will add the seconds to

'Epoch Timestamp' - this is the variable being passed into the expression. This expression expects the value to be an integer representing the number of seconds since 1970-1-1

'MM/dd/yyyy' - this is the ISO defined output format the user wants to represent the timestamp in. Either a single format specifier character or a custom format pattern that indicates how to format the value of this timestamp. If format is not provided, the ISO 8601 format ('o') is used. This is customizable to meet the needs of your date/time representation.

The first action is the Epoch time representation of May 11th, 2022. The second shows the expression to set the variable.The first action is the Epoch time representation of May 11th, 2022. The second shows the expression to set the variable.

NOTE: If you are working with values that are coming from a timestamp in a feature layer, the value returned is in milliseconds. Before passing it into the 'addSeconds' expression, divide the value by 1000.

7 Comments
MeganHicks
New Contributor II

Thanks for pointing me to this post, @SeanKMcGinnis! My question is, how does this work with attributes from the Fetch Updates function? I assume my progression needs to be When a record is updated -> Fetch updates -> convert time from unix to readable -> insert time into email and send. But I can't seem to figure out how to call my attributes for conversion into the function. 

When I try to add a step to Compose then Initialize Variable after the Fetch updates step, Power Automate says "The operation 'Initialize variable' can only be used at top level." and won't let me continue.

When I've converted time in a Survey 123 to email flow, my function looks like this:

addSeconds('1970-1-1', Div(triggerBody()?['feature']?['attributes']?['MyDateField'], 1000))
 
which is put into the Convert Time Zone built-in function, so I can see my dates in eastern time. 
 
When I try this same methodology with Fetch Updates, I get the error:
 
InvalidTemplate. Unable to process template language expressions in action 'Convert_time_zone' inputs at line '0' and column '0': 'The template language expression 'addSeconds('1970-1-1', Div(triggerBody()?['feature']?['attributes']?['DateTime'], 1000))' cannot be evaluated because property 'feature' cannot be selected. Array elements can only be selected using an integer index. Please see https://aka.ms/logicexpressions for usage details.'.
 
I suspect it would work if I knew what to add in the 'Epoch Timestamp' spot from your function, but I don't understand this data well enough to know. 
 
Thanks for any help you can provide on this!
 
MeganHicks_0-1682076603348.png

 

SeanKMcGinnis
Esri Contributor

@MeganHicks - send me an email (smcginnis@esri.com) and let's find 15 minutes to walk through what you are seeing and I will try to help address the issue you are encountering.

DominicRoberge2
Occasional Contributor III

This is what I am using from a Fetch update:

convertTimeZone(addSeconds('1970-1-1', Div(items('Apply_to_each_2')?['Attributes/EditDate'],1000),'MM/dd/yyyy hh:mm:ss tt'),'UTC','Central Standard Time','MM/dd/yyyy hh:mm:ss tt')

 

DominicRoberge2_0-1682083028814.png

output looks like this

DominicRoberge2_1-1682083062223.png

 

 

RobertAnderson3
MVP Regular Contributor

Thank you so much @SeanKMcGinnis for this post and @DominicRoberge2 for posting that code solution! It did exactly what I needed it to, I appreciate it so much!

For some reason when I was trying to make these expressions it wouldn't show me the attributes as dynamic content and trying to copy paste in the format of them from other spots wasn't working either (it was adding @{} around it) so I'm very glad to have found the solution!

NataliyaLys
Occasional Contributor II

I am new to power automate. I am try to send email when feature is updated. I'm able to get most of flow working, but I am stucked on date formatting. As part of dymanic content I would like to include 'CreationDate' field, but I keep getting and error;

 

'Unable to process template language expressions in action 'Convert_time_zone' inputs at line '0' and column '0': 'The template language function 'Div' expects its first parameter to be an integer or a decimal number. The provided value is of type 'Null'.'

 

This is the expression I put into Convert Time Zone widget

addSeconds('1970-1-1', Div(triggerBody()?['feature']?['attributes']?['CreationDate'], 1000))

 

Thank you,

 

 

SarahRijneke
Occasional Contributor

This works for me:

convertTimeZone(addSeconds('1970-1-1', Div(items('Apply_to_each_2')?['Attributes/CreationDate'],1000),'MM/dd/yyyy hh:mm:ss tt'),'UTC','Central Standard Time','MM/dd/yyyy hh:mm:ss tt')
SarahRijneke_0-1694461476532.png

 

SarahRijneke_0-1694461407736.png

 

NataliyaLys
Occasional Contributor II

Thank you @SarahRijneke your expression worked for me.