jsmreese / moment-duration-format

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

No minus sign for negative durations #71

Closed gordey4doronin closed 6 years ago

gordey4doronin commented 7 years ago

Hi,

Is it a bug? 👇

The following code:

function prettyDuration(start, end) {
  const duration = moment.duration(moment(end).diff(start));
  const prettyDuration = duration.format("d[d] h[h] m[m] s[s]");
  return prettyDuration;
}

const fixtures = [
  {
    start: new Date("2017-07-03T11:36:13.906Z"),
    end: new Date("2017-07-03T11:37:56.064Z"),
    result: "1m 42s"
  },
  {
    start: new Date("2017-07-03T11:36:13.906Z"),
    end: new Date("2017-07-03T12:37:56.064Z"),
    result: "1h 1m 42s"
  },
  {
    start: new Date("2017-07-03T11:36:13.906Z"),
    end: new Date("2017-07-04T12:37:56.064Z"),
    result: "1d 1h 1m 42s"
  }
];

console.log(prettyDuration(fixtures[0].start, fixtures[0].end));
console.log(prettyDuration(fixtures[1].start, fixtures[1].end));
console.log(prettyDuration(fixtures[2].start, fixtures[2].end));

console.log(prettyDuration(fixtures[0].end, fixtures[0].start));
console.log(prettyDuration(fixtures[1].end, fixtures[1].start));
console.log(prettyDuration(fixtures[2].end, fixtures[2].start));

Has the following output (should have three last rows with minus signs):

1m 42s
1h 1m 42s
1d 1h 1m 42s
1m 42s
1h 1m 42s
-1d 1h 1m 42s

CodePen playground: https://codepen.io/anon/pen/eRKVej?editors=0012

SpadarShut commented 7 years ago

I'll subscribe to this issue) Tried to fix it but it's really unclear where to fit it in the code and it breaks some tests

jsmreese commented 6 years ago

I'd done some work on negative durations some time ago... this issue is fixed in the plugin version on the master branch now.