How to get list of days in a month with Moment.js
Below are two nice functional approaches with no external dependency other than Moment:
const currentMonthDates = new Array(moment().daysInMonth()).fill(null).map((x, i) => moment().startOf('month').add(i, 'days'));
const currentMonthDates = Array.from({length: moment().daysInMonth()}, (x, i) => moment().startOf('month').add(i, 'days'));
This returns an array of Moment objects, you can then run whatever format method on it that you wish.
For further reading Creating and filling Arrays of arbitrary lengths in JavaScript, also note that in the first example you have to fill the array with null before mapping over it, as it is still classed as empty before doing so and therefore the map function would not run.
Alternative with momentjs, working for me
function getDaysArrayByMonth() {
var daysInMonth = moment().daysInMonth();
var arrDays = [];
while(daysInMonth) {
var current = moment().date(daysInMonth);
arrDays.push(current);
daysInMonth--;
}
return arrDays;
}
And you can check
var schedule = getDaysArrayByMonth();
schedule.forEach(function(item) {
console.log(item.format("DD/MM"));
});
Here's a function that will do the trick (not using Moment, but just vanilla JavaScript):
var getDaysArray = function(year, month) {
var monthIndex = month - 1; // 0..11 instead of 1..12
var names = [ 'sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat' ];
var date = new Date(year, monthIndex, 1);
var result = [];
while (date.getMonth() == monthIndex) {
result.push(date.getDate() + '-' + names[date.getDay()]);
date.setDate(date.getDate() + 1);
}
return result;
}
For example:
js> getDaysArray(2012,2)
["1-wed", "2-thu", "3-fri", "4-sat", "5-sun", "6-mon", "7-tue",
"8-wed", "9-thu", "10-fri", "11-sat", "12-sun", "13-mon", "14-tue",
"15-wed", "16-thu", "17-fri", "18-sat", "19-sun", "20-mon", "21-tue",
"22-wed", "23-thu", "24-fri", "25-sat", "26-sun", "27-mon", "28-tue",
"29-wed"]
ES2015+ version - also hid the array of names behind a closure so it's only initialized once:
const getDaysArray = (function() {
const names = Object.freeze([ 'sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat' ]);
return (year, month) => {
const monthIndex = month - 1
const date = new Date(year, monthIndex, 1);
const result = [];
while (date.getMonth() == monthIndex) {
result.push(`${date.getDate()}-${names[date.getDay()]}`);
date.setDate(date.getDate() + 1);
}
return result;
}
})();
As a side note, you can see that declaring the date const
doesn't keep us from mutating it (nor would Object.freeze
, used to make the weekday names array immutable, do anything to a Date
). We're taking advantage of the mutability here, but if we actually wanted an immutable Date with the language enforcing that immutability in current Javascript, we'd have to go to some length.
Also note that the solutions above don't zero-pad dates before the 10th, unlike the sample output included in the question. With ES2017+ that's pretty easy to fix:
result.push(`${date.getDate()}`.padStart(2,'0') + `-${names[date.getDay()]}`);
Doing it in older versions of JS requires rolling your own zero-padding logic, which isn't hard but is also not really the focus of the question.