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

Iterating range when end date is before start date does not work as expected. #239

Open
rhysawilliams2010 opened this issue Mar 29, 2018 · 2 comments
Labels
Milestone

Comments

@rhysawilliams2010
Copy link

rhysawilliams2010 commented Mar 29, 2018

The following code doesn't behave as expected:

      const start = moment("2018-03-26");
      const end = moment("2018-03-24");
      const range = moment.range(start, end);
      for(let day of range.by("day"))
      {
        console.log(day);
      }

outputs

moment("2018-03-26T00:00:00.000")
moment("2018-03-27T00:00:00.000")
moment("2018-03-28T00:00:00.000")

I would expect it to output

moment("2018-03-26T00:00:00.000")
moment("2018-03-25T00:00:00.000")
moment("2018-03-24T00:00:00.000")
@TristanJM
Copy link
Contributor

We have a PR open (targeting 5.0.0 release) which can be pushed into the upcoming 4.0.0 release that fixes this issue.

#201
This will automatically 'fix' your range so that the start is before the end moment.

If it is absolutely necessary for a backwards range, the user's application logic can be simply modified to remember if a range is backwards or forwards. The operations on a range should apply the same way regardless of time direction.

A fix for now would be to pre-sort that start < end before creating the range.

@TristanJM TristanJM added the bug label Mar 29, 2018
@TristanJM TristanJM added this to the 5.0.0 milestone Apr 6, 2018
@tsuijten
Copy link

I ran into the same issue, you can work around it by using range.snapTo("day").by("day")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants