here is the code:
range.js
export default function (n, m, polyfill = false, unit = "") {
let arr = []
for (let i = n; i <= m; iPP) {
let value = (polyfill && i < 10 ? "0" + i : i) + unit
arr.push(value)
}
return arr
}
date.js
import range from "./range";
function isLeapYear(y) {
return y % 4 === 0 && (y % 100 !== 0 || y % 400 === 0);
}
function getDays(y, m) {
y = Number(y);
m = Number(m);
let endDay = null;
switch (m) {
case 2:
endDay = isLeapYear(y) ? 29 : 28;
break;
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
endDay = 31;
break;
case 4:
case 6:
case 9:
case 11:
default:
endDay = 30;
break;
}
const days = range(1, endDay, false);
return days.map(day => {
return {
value: ("0" + day).length > 2 ? ("0" + day).substring(1) : "0" + day
};
});
}
const yearData = range(START_YEAR, END_YEAR, false);
const monthData = range(1, 12, false);
const cascadeMonthData = monthData.map(month => {
return {
value: ("0" + month).length > 2 ? ("0" + month).substring(1) : "0" + month,
children: []
};
});
const dateData = yearData.map(year => {
const item = {
value: year,
children: cascadeMonthData.slice()
};
item.children.forEach(month => {
month.children = getDays(year, month.value);
});
return item;
});
console.log(dateData)