Skip to content

Instantly share code, notes, and snippets.

@paulwib
Created August 1, 2024 16:41
Show Gist options
  • Save paulwib/2706eaed1234797ea2f9edda22ff619c to your computer and use it in GitHub Desktop.
Save paulwib/2706eaed1234797ea2f9edda22ff619c to your computer and use it in GitHub Desktop.
Generate lists of years, months and days
import * as R from 'ramda';
// Used for HTML form where you can select individual year, month and day from dropdowns
// Last 20 years in reverse order
const years: number[] = R.unfold(
(n: number) => n < new Date().getFullYear() - 20 || [n, n - 1],
new Date().getFullYear(),
);
// All the calendar months
const months: number[] = R.unfold(
(n: number) =>
n > 11 ? false : [new Date(0, n).toLocaleString('en-US', { month: 'long' }), n + 1],
0,
);
// 31 days - tweaking the number of days based on month selected would require additional scripting
const days: number[] = R.unfold((n: number) => n > 31 || [n, n + 1], 1);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment