icholy / Duration.js

MIT License
185 stars 14 forks source link

Humanize duration object #13

Closed nstanard closed 7 years ago

nstanard commented 7 years ago

I'd like to propose the functionality of converting the duration object back into the readable string.

For instance: var d = new Duration("6w5d4h3m2s1ms"); d.humanizeDuration(format);

humanizeDuration = function that takes a format string value. format = a string to specify the output format. E.g. "wdh" or "dh"

"wdh" returns "6w5d4h" "dh" return "47d4h"

I will be taking a stab at adding this feature and opening a PR but wanted to run this by an issue to get feedback first. If you have any concerns with this functionality I'd love to talk about that before I really dig in.

icholy commented 7 years ago

Instead of adding an additional method, toString() should take an optional format parameter which defaults to a format that maintains the current behaviour.

There's a problem though: ms. Is that minute second or millisecond? The format you're describing is ambiguous.

nstanard commented 7 years ago

Instead of adding an additional method, toString() should take an optional format parameter which defaults to a format that maintains the current behaviour.

nstanard commented 7 years ago

There's a problem, though: ms. Is that minute second or millisecond? The format you're describing is ambiguous.

Great point. Thank you for responding so quickly. What about using case sensitivity?

WDHMSMs

I'm honestly not in love with that but I'll still pull this down sometime soon and mess with the idea.

icholy commented 7 years ago

I don't like the case sensitive format either. I was thinking comma separated w,d,h,m,s,ms, but that's ugly too. There could be fully fledged formatting: {w} week(s) {d} day(s) {s} second(s). But that would require implementing a templating language (I'm not willing to pull in a dependency). However, this seems like too much and beyond the scope of this project.

nstanard commented 7 years ago

However, this seems like too much and beyond the scope of this project.

I understand if you feel that way. I've already converted it where I needed it but thought it would come in really handy. I'm using this module to convert only for display purposes. So being able to pass a format and get that back from the object would simply save me the logic of getting the remainder and formatting it myself. (I have a text input that on blur has it's format updated/converted. This allows the user to be able to input 26h or 1d2h)

This is not a huge priority for me right now though either. If I can come up with a format that I think you'll like I'll open a PR.

StefanBrand commented 5 years ago

There is a widespread date format language called strftime. It is used by Liquid, for example.

icholy commented 5 years ago

@StefanBrand how does that work with duration formatting though?

StefanBrand commented 5 years ago

For milliseconds they use %L or %3. I now get that I had something different in mind (outputting a formatted string as discussed above), but instead of "ms" you could still align with strftime in that you use "l" as an identifier for milliseconds. It's not very intuitive, I admit, but it solves the ambiguity.