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

Array.from(range.by('year')) lost last year #292

Open
mouday opened this issue Jul 13, 2022 · 1 comment
Open

Array.from(range.by('year')) lost last year #292

mouday opened this issue Jul 13, 2022 · 1 comment

Comments

@mouday
Copy link

mouday commented Jul 13, 2022

const range = moment.range('2014-11-01', '2015-02-01');

const years = Array.from(range.by('year')).map(
    month => month.year()
);
console.log(years);
// [ 2014 ]

i wish get [ 2014, 2015 ], but get [ 2014 ]

@rcanoaparicio
Copy link

Hi @mouday!

When using range.by('year') each step is apart from each other by a year. In your example the next value for '2014-11-01' would be '2015-11-01'. Since '2015-02-01' < '2015-11-01' it will just return the first value.

In your case you can try something like:

const begin = moment('2014-11-01').startOf('year');
const end = moment('2015-02-01').startOf('year');
const range = moment.range(begin, end);
const years = Array.from(range.by('year')).map(
    date => date.year()
);
console.log(years);

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

2 participants