Skip to content

moment().diff(moment()) does not always return 0 #5195

@jo-flynn

Description

@jo-flynn

Describe the bug
When repeatedly running this calculation: moment().add(1, 'years').diff(moment(), 'days') when the current date is in a year before a leap year (in my case, 2019/08/13), moment inconsistently returns 365 or 366. Adding .startOf('day') to each moment() will consistently return 366.

To Reproduce
Steps to reproduce the behavior:

  1. Execute moment().add(1, 'years').diff(moment(), 'days') at least 4 times.
  2. Compare outputs, you'll see 365 and 366 inconsistently.
  3. Execute moment().startOf('day').add(1, 'years').diff(moment().startOf('day'), 'days') at least 4 times.
  4. Compare outputs, you should see 366 each time.

Expected behavior
moment should consistently account for leap years when calculating day differences.

Desktop (please complete the following information):

  • OS: Windows & MacOS
  • Browser: Chrome
  • Version 76

Moment-specific environment

  • The time zone setting of the machine the code is running on: PST
  • The time and date at which the code was run: See below
  • Other libraries in use (TypeScript, Immutable.js, etc): Bug occurred in a CRA React app, but is replicable on https://momentjs.com in the console.

Please run the following code in your environment and include the output:

console.log((new Date()).toString())
console.log((new Date()).toLocaleString())
console.log((new Date()).getTimezoneOffset())
console.log(navigator.userAgent)
console.log(moment.version)

Output:

Tue Aug 13 2019 12:30:35 GMT-0700 (Pacific Daylight Time)
8/13/2019, 12:30:35 PM
420
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36
2.24.0

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions