codebox / moment-precise-range

A moment.js plugin to display human-readable date/time ranges
https://codebox.net/pages/moment-date-range-plugin
MIT License
150 stars 91 forks source link

Consider making this into a generic duration formatter #9

Closed jpuffer closed 8 years ago

jpuffer commented 9 years ago

It would be great if this was more generic so it could be used with any duration, not just a duration coming from a range. Consider the use case where I already have a duration of time (not two times) that I want formatted into days, hours, minutes etc. I can currently utilize this plugin in the following way:

$scope.formatDuration = function(minutes){
  var now = moment();
  var nowPlusDuration = moment().add(minutes, 'minutes');
  return moment.preciseDiff(now, nowPlusDuration);
};

A more general-purpose plugin based on moment-precise-range could be consumed like so:

$scope.formatDuration = function(minutes){
  var seconds = minutes * 60;
  return moment.preciseReadableDuration(seconds);
};

The general-purpose way could still easily be consumed with ranges by doing the range part with native Moment diff like so:

$scope.formatRange = function(time1, time2){
  var seconds = time1.diff(time2, 'seconds');
  return moment.preciseReadableDuration(seconds);
};
codebox commented 8 years ago

One problem I can think of with this is that the same span of time will be expressed differently depending on the starting date, for example a duration of 30 days beginning on 1st Jan would just be '30 days' but the same duration starting on 1st Feb might be '1 month and 2 days'