Skip to content

Instantly share code, notes, and snippets.

@jagomf
Last active February 6, 2020 10:22
Show Gist options
  • Save jagomf/07729b4a341ad74b596cd0a9da694e29 to your computer and use it in GitHub Desktop.
Save jagomf/07729b4a341ad74b596cd0a9da694e29 to your computer and use it in GitHub Desktop.
Generate constants with months/weekdays names
// More info on locales and codes for names on MDN 'toLocaleString'
weekdaysArray(locale = 'en-us', startMonday = false) {
const initYear = 2006; // Year that started on the first day of week
return Array.from({ length: 7 }, (v, k) => k)
.map(weekday => new Date(initYear, 0, weekday + (startMonday ? 1 : 0) + 1)
.toLocaleString(locale, { weekday: 'narrow' }));
}
monthsArray(locale = 'en-us') {
return Array.from({ length: 12 }, (v, k) => k)
.map(month => new Date(2000, month, 1)
.toLocaleString(locale, { month: 'long' }));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment