84669 person learning
152542 person learning
20005 person learning
5487 person learning
7821 person learning
359900 person learning
3350 person learning
180660 person learning
48569 person learning
18603 person learning
40936 person learning
1549 person learning
1183 person learning
32909 person learning
比如说我的是“2017-01”和“2017-05”我想遍历输出全部的月份,怎样写才能输出["2017-01","2017-02","2017-03","2017-04","2017-05"]还有就是存在跨年的情况例如“2017-11”和“2018-03”怎样输出["2017-11","2017-12","2018-01","2018-02","2018-03"]
How to traverse all the months-PHP Chinese website Q&A-How to traverse all the months-PHP Chinese website Q&A
Look around and learn.
这是我的实现方式,仅供参考
function traverse(start, end) { start = start.split('-'); end = end.split('-'); var startYear = parseInt(start[0]), startMon = parseInt(start[1]), endYear = parseInt(end[0]), endMon = parseInt(end[1]); if(startMon<1 || startMon>12){ throw new Error('初始月份不正确'); } if(endMon<1 || endMon>12){ throw new Error('终止月份不正确'); } var result = []; var year, mon; for (year = startYear; year < endYear;) { for(mon = startMon; mon<=12; mon++){ result.push(year +'-'+ (mon<10?'0'+mon:mon)); if(mon == 12) year++; } } if(startYear == endYear){ mon = startMon; }else{ mon = 1; } for (; mon <= endMon; mon++) { result.push(endYear +'-'+ (mon<10?'0'+mon:mon)); } return result; }
How to traverse all the months-PHP Chinese website Q&A-How to traverse all the months-PHP Chinese website Q&A
Look around and learn.
这是我的实现方式,仅供参考