Created
October 29, 2020 23:23
-
-
Save lcherone/3ce7447bbaeacddd1d02be8beb9b1d43 to your computer and use it in GitHub Desktop.
range js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const range = (start, end, step) => { | |
const range = [] | |
typeof step === 'undefined' && (step = 1) | |
if (end < start) step = -step | |
while (step > 0 ? end >= start : end <= start) { | |
range.push(start) | |
start += step | |
} | |
return range | |
} | |
// [1,2,3...] | |
let days = range(1, 31) | |
// ["January", "February", "March",...] | |
let months = range(1, 12).map(v => | |
new Date(1970, v - 1, 1).toLocaleString('en-us', { | |
month: 'long' | |
}) | |
) | |
// [2020,2019,2018,...,2010] | |
let years = range(new Date().getFullYear(), new Date().getFullYear() - 10) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment