/**
* @param {int} The month number, 0 based
* @param {int} The year, not zero based, required to account for leap years
* @return {Date[]} List with date objects for each day of the month
*/
function getDaysInMonthUTC(month, year) {
var date = new Date(Date.UTC(year, month, 1));
var days = [];
while (date.getUTCMonth() === month) {
days.push(new Date(date));
date.setUTCDate(date.getUTCDate() + 1);
}
return days;
}
function getDaysInMonth(month, year) {
var date = new Date(year, month, 1);
var days = [];
while (date.getMonth() === month) {
days.push(new Date(date));
date.setDate(date.getDate() + 1);
}
return days;
}
const days2020 = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
const days2021 = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
describe("getDaysInMonthUTC", function() {
it("gets day counts for leap years", function() {
const actual = days2020.map(
(day, index) => getDaysInMonthUTC(index, 2020).length
);
expect(actual).toEqual(days2020);
});
it("gets day counts for non-leap years", function() {
const actual = days2021.map(
(day, index) => getDaysInMonthUTC(index, 2021).length
);
expect(actual).toEqual(days2021);
});
});
describe("getDaysInMonth", function() {
it("gets day counts for leap years", function() {
const actual = days2020.map(
(day, index) => getDaysInMonth(index, 2020).length
);
expect(actual).toEqual(days2020);
});
it("gets day counts for non-leap years", function() {
const actual = days2021.map(
(day, index) => getDaysInMonth(index, 2021).length
);
expect(actual).toEqual(days2021);
});
});
// load jasmine htmlReporter
(function() {
var env = jasmine.getEnv();
env.addReporter(new jasmine.HtmlReporter());
env.execute();
}());
<script src="https://cdn.jsdelivr.net/jasmine/1.3.1/jasmine.js"></script>
<script src="https://cdn.jsdelivr.net/jasmine/1.3.1/jasmine-html.js"></script>
<link href="https://cdn.jsdelivr.net/jasmine/1.3.1/jasmine.css" rel="stylesheet"/>