jquery 在JavaScript中获取给定日期范围内的所有日期列表

xjreopfe  于 2023-10-17  发布在  jQuery
关注(0)|答案(3)|浏览(90)

我试图找出是否有任何可用的帮助函数或任何可能有助于获得给定日期范围内的日期列表的技术。
假设用户输入以下参数,这将是一个2年的合同:

**日期:**15
**开始日期:**1/15/2015
**结束日期:**12/15/2016

我希望返回该期间内每个月的日期,包括开始和结束月份:

1/15/2015
2/15/2015
3/15/2015
.....
1/15/2016
2/15/2016
3/15/2016
.....
12/15/2016
deyfvvtc

deyfvvtc1#

你需要使用setMonthgetMonth方法:

**注意:**正如@RobG在评论中所说,我犯了一个错误,使用了这样的日期格式:2015-12-01为例。使用-is not interpreted by all browsers的日期格式。最好使用/字符。

const start = new Date('2015-01-15');
const end = new Date('2016-12-15');

while (start <= end) {
    console.log( new Date(start) );
    start.setMonth( start.getMonth() + 1 );
}

更动态的解决方案:

function getDatesBtween(from, to, day) {

  const fromDate = new Date(from);
  const toDate = new Date(to);

  fromDate.setDate(day);
  toDate.setDate(day);

  const dates = [];

  while (fromDate <= toDate) {

    dates.push(new Date(fromDate));

    fromDate.setMonth(fromDate.getMonth() + 1);

  }

  return dates;

}

const dates = getDatesBtween('2015-01-15', '2016-12-15', 15);

console.log(dates);

**注意:**正如@HBP在评论中提到的,上述解决方案没有考虑边缘情况,并且不适用于一个月的最后几天(29th30th31st)。例如,在某些情况下2015/02/31 = 2015/03/03,在其他情况下2015/03/02(闰年)。下一个解决方案解决了这个问题:

function DateManager() {

  // Create a date with specific day
  function setDate(date, day) {

    date = new Date(date);
    date.setDate(day);
    date.setHours(23);

    return date;

  }

  // Generate dates method
  this.getDatesBetween = function(date1, date2, day) {

    const range1 = new Date(date1);
    const range2 = new Date(date2);
    const dates = [];
    let temp = null;
    
    date1 = setDate(date1, day);
    date2 = setDate(date2, day);

    while (date1 <= date2) {

      if (date1.getDate() != day) {

        temp = setDate(date1, 0);

        if (temp >= range1 && temp <= range2) dates.push(temp);

        date1 = setDate(date1, day);

      } else {

        temp = new Date(date1);

        if (temp >= range1 && temp <= range2) dates.push(temp);

        date1.setMonth(date1.getMonth() + 1);

      }

    }

    return dates;

  };

}

const manager = new DateManager();
const dates = manager.getDatesBetween('2015-01-15', '2016-12-15', 31);

console.log(dates);

结果将类似于:

2015/01/31
2015/02/28
2015/03/31
2015/04/30
2015/05/31
...
2016/02/29
...
2016/11/30
5fjcxozz

5fjcxozz2#

$("#from").datepicker();
$("#to").datepicker();


$('#getBetween').on('click', function () {
    var start = $("#from").datepicker("getDate"),
        end = $("#to").datepicker("getDate"),
        currentDate = new Date(start),
        between = []
    ;

    while (currentDate <= end) {
        between.push(new Date(currentDate));
        currentDate.setMonth(currentDate.getMonth() + 1);
    }
    
    $('#results').html(between.join('<br> '));
});
<link href="http://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css" rel="stylesheet">
      <script src="http://code.jquery.com/jquery-1.10.2.js"></script>
      <script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
<input id="from" />
<input id="to" />
<button id="getBetween">Get Between Dates</button>
<div id="results"></div>
acruukt9

acruukt93#

我认为getDate()+ 1应该能够解决边界条件。

var startDate = new Date("2020-02-01"); //YYYY-MM-DD
var endDate = new Date("2020-03-07"); //YYYY-MM-DD

var getDateArray = function(start, end) {
    var arr = new Array();
    var dt = new Date(start);
    while (dt <= end) {
        arr.push(new Date(dt));
        dt.setDate(dt.getDate() + 1);
    }
    return arr;
}

var dateArr = getDateArray(startDate, endDate);

// Output
document.write("Start Date: " + startDate + "<br>");
document.write("End Date: " + endDate + "<br>");
document.write("Date Array<br>")
for (var i = 0; i < dateArr.length; i++) {
    document.write(dateArr[i] + "<br>");
}

相关问题