常用工具函数

日期

【日期】根据时间范围 返回 范围内全部日期

/**
 * 【日期】根据时间范围 返回 范围内全部日期
 * @description 支持 跨年 跨月
 * @param {string} start 起始日期 "YYYY-MM-DD"
 * @param {string} end 结束日期 "YYYY-MM-DD"
 * @returns {array} ['xxxx-xx-xx', 'xxxx-xx-xx', ...]
 */ 
function getBetweenDateStr(start, end) {
  var result = [];
  var beginDay = start.split("-");
  var endDay = end.split("-");
  var diffDay = new Date();
  var dateList = new Array();
  var i = 0;
  diffDay.setDate(beginDay[2]);
  diffDay.setMonth(beginDay[1] - 1);
  diffDay.setFullYear(beginDay[0]);
  result.push(start);
  while (i == 0) {
    var countDay = diffDay.getTime() + 24 * 60 * 60 * 1000;
    diffDay.setTime(countDay);
    dateList[2] = diffDay.getDate();
    dateList[1] = diffDay.getMonth() + 1;
    dateList[0] = diffDay.getFullYear();
    if (String(dateList[1]).length == 1) {
      dateList[1] = "0" + dateList[1];
    }
    if (String(dateList[2]).length == 1) {
      dateList[2] = "0" + dateList[2];
    }
    result.push(dateList[0] + "-" + dateList[1] + "-" + dateList[2]);
    if (
      dateList[0] == endDay[0] &&
      dateList[1] == endDay[1] &&
      dateList[2] == endDay[2]
    ) {
      i = 1;
    }
  }
  return result;
}

getBetweenDateStr("2021-09-25", "2021-10-05")

// ['2021-09-25', '2021-09-26', '2021-09-27', '2021-09-28', '2021-09-29', '2021-09-30', '2021-10-01', '2021-10-02', '2021-10-03', '2021-10-04', '2021-10-05']

最后更新时间:
贡献者: DESKTOP-ER5718D\zt