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

问题描述 投票:0回答:3

我试图弄清楚是否有任何可用的辅助函数或任何可能有助于获取给定日期范围内的日期列表的技术。

假设用户输入以下参数,这将是 2 年合同:
日:15
开始日期: 2015 年 1 月 15 日
结束日期: 2016 年 12 月 15 日

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

1/15/2015
2/15/2015
3/15/2015
.....
1/15/2016
2/15/2016
3/15/2016
.....
12/15/2016
javascript jquery date
3个回答
3
投票

您需要使用

setMonth
getMonth
方法:

注意: 正如@RobG 在评论中所说,我使用这样的日期格式犯了一个错误:

2015-12-01
作为示例。使用
-
的日期格式并非所有浏览器都能解释。最好使用
/
字符来代替

注释 2: 2024 年 10 月 18 日:上述注释不再正确。 文档指出使用斜杠的日期是非标准的(即使它们被接受并在所有浏览器中工作)。

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 在评论中提到的,上述解决方案没有考虑边缘情况,并且不适用于一个月的最后几天(

29th
30th
31st
)月)。例如,在某些情况下
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

1
投票

$("#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>


1
投票

我认为 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>");
}
© www.soinside.com 2019 - 2024. All rights reserved.