Search code examples
freemarkeriso8601

Convert seconds to ISO8601 duration in Freemarker


How to convert seconds to ISO 8601 Duration in Freemarker?

For eg. 140 seconds -> PT2M20S

Is there any freemarker builtins that can come in handy here? Or String manipulation?


Solution

  • I ended up adding this function:

    <#function convertDurationSeconds seconds>
        <#local mins = (seconds/60)?int />
        <#local seconds = (seconds%60) />
        <#return 'PT' + (mins > 0)?then(mins + 'M','') + (seconds > 0)?then(seconds + 'S','') />
    </#function>
    

    Note: I needed it for min and seconds only since it was for short video durations, this function can be extended to represent other time intervals (Y, M(month), W, D, M, and S)