jsmreese / moment-duration-format

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

Sometimes rounding results in a negative zero (-0) #156

Open tncbbthositg opened 2 years ago

tncbbthositg commented 2 years ago

In this code for example:

const moment = require('moment');
const momentDurationFormat = require("moment-duration-format")

const endDate = moment('2022-03-18');
const startDate = moment('2021-11-16');

const duration = moment.duration(endDate.diff(startDate));
console.log(duration.asDays());

console.log(duration.format("M __ D __", { trim: 'all' }));

The output is:

121.95833333333333
"4 months -0 days"

This is the case in moment-duration-format v2.3.2.

I can work around this like so:

  const diff = endDate.diff(startDate);
  const wholeDayDuration = moment.duration(Math.round(diff / 86400000) * 86400000);

Effectively, the missing hour due to the daylight savings time change is causing a -0 to show up in the duration format.