Skip to content

Instantly share code, notes, and snippets.

@lcherone
Created October 29, 2020 23:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lcherone/3ce7447bbaeacddd1d02be8beb9b1d43 to your computer and use it in GitHub Desktop.
Save lcherone/3ce7447bbaeacddd1d02be8beb9b1d43 to your computer and use it in GitHub Desktop.
range js
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