Support Questions

Find answers, ask questions, and share your expertise
Announcements
Celebrating as our community reaches 100,000 members! Thank you!

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

avatar
New Contributor

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)?

1 ACCEPTED SOLUTION

avatar

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.

View solution in original post

3 REPLIES 3

avatar

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.

avatar
Super Mentor

@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

avatar
Community Manager

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



Regards,

Vidya Sargur,
Community Manager


Was your question answered? Make sure to mark the answer as the accepted solution.
If you find a reply useful, say thanks by clicking on the thumbs up button.
Learn more about the Cloudera Community: