jsmreese / moment-duration-format

Format function plugin for the Moment Duration object.
MIT License
968 stars 120 forks source link

Formatting for Non-Standard Durations #65

Open matt-easyagile opened 7 years ago

matt-easyagile commented 7 years ago

Hi, I have a case where I need to format a duration based on a custom definition i.e. 8 hours = 1 day & 5 days = 1 week. So that (for example); 25 hours would be 3 days and 1 hour (not 1 day and 1 hour). Is there any way to get your plugin to do this, or do you know of any other moment plugins that already do it?

jsmreese commented 7 years ago

Now that's a very interesting use case.

8h = 1d and 5d = 1w Are there any other custom timeperiod definitions involved? What is your input like? Is your input always in one specific unit?

My plugin uses moment itself under the hood to calculate token values... it's possible you could simply modify those relationships on an instance of moment? It's been a while since I've looked at the relevant code.

matt-easyagile commented 7 years ago

This is to specify durations for business day activities (hence the 8hr days and 5 day weeks), but actually in the application the definition of business hours is user configurable so should be able to handle other values too. The input is always a duration in milliseconds (i.e. 1 day 2 hours = {numberOfHoursPerDay} hours 120 minutes 60 seconds * 1000 milliseconds).

I've had a look at the momentjs docs, but from what I understand, the assumptions for the definition of day and week length is hardcoded into duration methods. Perhaps there is some other way of passing a config into an instance of moment. Will keep searching. Thanks!!

Mihailoff commented 5 years ago

@jsmreese I have the same issue and wonder if there any workarounds for it. The base absolutely valid assumption is that it is 24h in a day, however, for business hours it's not true.

jsmreese commented 5 years ago

@Mihailoff It's definitely a valid use case for Durations, unfortunately I haven't done any digging myself!

Would be a fun problem to work on though... life is busy these days but maybe I can give it some thought. What about building a standalone configurable Duration object so you could configure days/week and hours/day? That might be possible without too much modification of moment itself?

It's just days/week and hours/day that need to be configured, right? All other conversions could remain the same (not that they're ideal... going from days->years and back again can be problematic, but that's a known moment issue!)

jsmreese commented 5 years ago

This is fundamentally a core Moment issue, but I'll go ahead and reopen this. Maybe there's a way we can turn this feature into a patch or plugin.

jsmreese commented 5 years ago

Has anyone needed to format durations around the 4-5-4 retail calendar? Where months are exactly 4 weeks or 5 weeks long, and years are exactly 52 weeks or 53 weeks long?