ioBroker.javascript icon indicating copy to clipboard operation
ioBroker.javascript copied to clipboard

add "format_duration" function

Open Marty56 opened this issue 3 years ago • 2 comments

The "formatDate" function is already supported the javascript adapter. It would be great if a "format_duration" function could be added that formats a duration (in milliseconds) to a string. The example below is specific for German and should be enhanced for international usage.

function format_duration(dauer,format_string) {
    const tl = 60000*60*24
    var days = Math.floor(dauer/tl)
    return ((days !== 0) ? days + " Tg " : "") + formatDate((dauer+1) %  tl,format_string) + " h"
}

Marty56 avatar Jun 10 '22 08:06 Marty56

But in fact then abit more would be needed or?!

"37d 7h 5m 2s"? ;-)

if not going higher also also suporting years and months, but this gets problematic ... so easier to "just" start on days, or?

Apollon77 avatar Jun 10 '22 08:06 Apollon77

Sure!

Supporting years and months (what is a month anyway?) would be an overkill TMH and I would refrain from that. Flexibility on the units with respect of language would be necessary. And the format you have proposed would also be better.

PS: Maybe the module "format-duration" would also work and leave the final formatting of units to the user.

Marty56 avatar Jun 10 '22 10:06 Marty56