Skip to content

Instantly share code, notes, and snippets.

@dazecoop
Last active November 21, 2022 16:04
Show Gist options
  • Save dazecoop/61b5fcf1b33ea18c0e53fb7ba69cc4ec to your computer and use it in GitHub Desktop.
Save dazecoop/61b5fcf1b33ea18c0e53fb7ba69cc4ec to your computer and use it in GitHub Desktop.
Vanilla JS days of week as an array of objects. (Mon, Tues, Weds) or (Monday, Tuesday, Wednesday) format. Localization support
const days = Array(7)
.fill(new Date())
.map((el, i) =>
new Date(el.setDate(el.getDate() - el.getDay() + i))
.toLocaleString('en', { weekday: 'short' })) // change 'en' for different locales, or 'short'/'long' for short/long weekday titles
.map((label, value) => {
return {
value,
label,
}
})
days.push(days.shift()) // Optionally start week on Monday rather than Sunday
console.log(days)
/** @output
[
{
"value": 1,
"label": "Mon"
},
{
"value": 2,
"label": "Tue"
},
{
"value": 3,
"label": "Wed"
},
{
"value": 4,
"label": "Thu"
},
{
"value": 5,
"label": "Fri"
},
{
"value": 6,
"label": "Sat"
},
{
"value": 0,
"label": "Sun"
}
]
**/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment