Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
tncbbthositg opened this issue Mar 18, 2022 · 0 comments
Open

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

tncbbthositg opened this issue Mar 18, 2022 · 0 comments

Comments

@tncbbthositg
Copy link

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.

@tncbbthositg tncbbthositg changed the title Sometimes the rounding results in a negative zero (-0) Sometimes rounding results in a negative zero (-0) Mar 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant