Reputation: 958
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.
Upvotes: 18
Views: 39353
Reputation: 121
I use this solution:
var defaultWeekdays = Array.apply(null, Array(7)).map(function (_, i) {
return moment(i, 'e').startOf('week').isoWeekday(i + 1).format('ddd');
});
I got result:
["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
You can Modify .format(string) to change the days format. E.g 'dddd' will shows:
["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
Check Moment.js documentation for more advanced format
Upvotes: 8
Reputation: 748
For weekdays, you could use moment's weekdays method
weekArray = moment.weekdays()
Upvotes: 62
Reputation: 10204
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');
});
console.log(weekdayNames);
// 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')
.
Upvotes: 1