Moment: .diff() returns negative value

Created on 2 Nov 2017  ·  3Comments  ·  Source: moment/moment

Description of the Issue and Steps to Reproduce:
execute moment('2016-02-01').diff('2016-03-01', 'days')

Environment:
react 1.6

Examples: Chrome 49 on OSX, Internet Explorer 10 on Windows 7, Node.JS 4.4.4 on Ubuntu 16.0.4
chrome

Basically, I wanted to calculate days between these two dates and the result is -29.

Most helpful comment

@stackninigi Positive vs negative results from diff let you know which date is earlier / later than the other.

If you wanted a positive result, you should ensure you do later.diff(earlier, ...)
i.e. moment('2016-03-01').diff('2016-02-01', 'days')

All 3 comments

This is achievable through another way.

moment.duration(moment('2016-02-01').diff(moment('2016-03-01'))).asDays()

@stackninigi Positive vs negative results from diff let you know which date is earlier / later than the other.

If you wanted a positive result, you should ensure you do later.diff(earlier, ...)
i.e. moment('2016-03-01').diff('2016-02-01', 'days')

What @ashsearle said. I would take the Math.abs() of the result.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

danieljsinclair picture danieljsinclair  ·  3Comments

vbullinger picture vbullinger  ·  3Comments

IbraheemAlSaady picture IbraheemAlSaady  ·  3Comments

nikocraft picture nikocraft  ·  3Comments

benhathaway picture benhathaway  ·  3Comments