Skip to main content
GSB
Bronze III
Mar 6, 2023 4:50 PM UTC
Solved

Is there a way to convert seconds to HH:mm format?

I have a feed coming in with time spent value set in seconds. Example 900 or 3600

Is there way in nifi to convert attribute to 00:15 and 01:00 (HH:mm format)?

cotopaul
cotopaulAnswer
Platinum ⭐️
Mar 7, 2023 9:04 AM UTC

I would try something like:

 

${value:divide(3600)}:${value:divide(60):mod(60)}

 

value = your attribute.
value:divide(3600) = identify the hours.
value:divide(60):mod(60) = identify the minutes.
Give it a try and let me know if it works fine for you 😀

LE:
If you want the leading 0 as well, if the hour value is lower then 10, try something like:

${value:divide(3600):lt(10):ifElse(${value:divide(3600):prepend(0)},${value:divide(3600)})}:${value:divide(60):mod(60)}

It is basically the same thing as before, but instead, you are using an IF-ELSE to check whether the the value for the hours is lower then 10 and if so, you append a leading 0 to it so it will display as 0X:MM. If the value is greater then 10, you stick with the original value, without adding any new zeros.

3 replies

cotopaul
cotopaulAnswer
Platinum ⭐️
Mar 7, 2023 9:04 AM UTC

I would try something like:

 

${value:divide(3600)}:${value:divide(60):mod(60)}

 

value = your attribute.
value:divide(3600) = identify the hours.
value:divide(60):mod(60) = identify the minutes.
Give it a try and let me know if it works fine for you 😀

LE:
If you want the leading 0 as well, if the hour value is lower then 10, try something like:

${value:divide(3600):lt(10):ifElse(${value:divide(3600):prepend(0)},${value:divide(3600)})}:${value:divide(60):mod(60)}

It is basically the same thing as before, but instead, you are using an IF-ELSE to check whether the the value for the hours is lower then 10 and if so, you append a leading 0 to it so it will display as 0X:MM. If the value is greater then 10, you stick with the original value, without adding any new zeros.

Translate
MattWho
Titanium ⭐️⭐️
Titanium ⭐️⭐️
Mar 8, 2023 3:44 PM UTC

@GSB 

If you wanted it always to be two digits, you would need to apply the same if/else NiFi Expression Language (NEL) logic the minute calculations in the working solution provided by @cotopaul 

${value:divide(3600):lt(10):ifElse(${value:divide(3600):prepend(0)},${value:divide(3600)})}:${value:divide(60):mod(60):lt(10):ifElse(${value:divide(3600):mod(60):prepend(0)},${value:divide(3600):mod(60)})}


A simpler approach would be to use the toDate and Format NEL functions:

${value:toDate('sssss'):format('HH:mm')}

I allow 5 's' assuming that max value would be 86,500 seconds (24 hours in a day) and does not matter if value is smaller.   This format also allows you to quickly and easily adjust format for example, maybe you don't want to truncate the remaining seconds and use ":format('HH:mm:ss')" instead.

If you found that the provided solution(s) assisted you with your query, please take a moment to login and click Accept as Solution below each response that helped.

Thank you,

Matt

Translate
VidyaSargur
Community Manager
Community Manager
Mar 13, 2023 6:09 AM UTC

@GSB Have any of the replies helped resolve your issue? If so, please mark the appropriate reply as the solution, as it will make it easier for others to find the answer in the future. 

Translate
Regards, Vidya Sargur Madabhushi| Senior Community Manager | Was your question answered? Make sure to mark the answer as the Best Answer. If you find a reply useful, say thanks by clicking on the thumbs up button.