Using moment js to create an array with days of the week and hours of the day?


Wondering if there was a way to get momentjs or just use pure javascript to create an array of everyday of the week, and every hour in a day so that I dont have to hardcode it.

So instead of manually doing

weekArray = ["Monday", "Tuesday", "Wednesday" ....]

I'm looking for a way to do something like

weekArray = moment.js(week)

The same idea for times during the day especially, so I could potentially use different formats.

Problem courtesy of: grasshopper


For weekdays, you could use moment's weekdays method

weekArray = moment.weekdays()
Solution courtesy of: Quentin


Here's a little snippet to get the (locale-specific) names of the days of the week from Moment.js:

var weekdayNames = Array.apply(null, Array(7)).map(
    function (_, i) {
        return moment(i, 'e').format('dddd');
// Array [ "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" ]

If you want the week to start on Monday, replace moment(i, 'e') with moment(i+1, 'e').

Discussion courtesy of: Robert K. Bell

This recipe can be found in it's original form on Stack Over Flow.