Skip to content

moment.min returns different results depending on the order #2362

Description

@cwohlman

If I try to get the min of two moments, one of which is invalid, then the result I get depends on the order in which I specify the moments:

moment.min(moment(""), moment()).toDate()
// Invalid Date
moment.min(moment(), moment("")).toDate()
// Wed May 06 2015 14:52:20 GMT-0500 (CDT)

I'm not sure what the expected behavior should be, but what I expected is something like this:

function getMin() {
  var dates = Array.prototype.slice.call(arguments);
  dates = dates.filter(function (a) {return a.isValid();});
  // etc.
}

Activity

  1. cwohlman commented on May 6, 2015

    @cwohlman
    ContributorAuthor
  2. mattjohnsonpint commented on Jun 23, 2015

    @mattjohnsonpint
    Contributor

    Consider:

    • Moments are compared based on their value.
    • moment("").valueOf() returns NaN
    • Math.min(NaN,0) and Math.min(0,NaN) return NaN

    Therefore, I would expect any moment compared to an invalid moment to return the invalid moment.

  3. cwohlman commented on Jun 23, 2015

    @cwohlman
    ContributorAuthor

    That sounds right to me, not necessarily what I would expect, but certainly logical and predictable.

  4. ichernev commented on Jun 26, 2015

    @ichernev
    Contributor

    Well, PRs is welcome :)

  5. ichernev commented on Jul 8, 2015

    @ichernev
    Contributor

    Close in favor of #2438

    Note that you still have to filter out invalid moments yourself, or you'll get some invalid result (one of the args).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions