[英]Getting a list of all dates within a given date range in JavaScript
我試圖弄清楚是否有任何可用的輔助函數或任何可能有助於獲取給定日期范圍內的日期列表的技術。
假設用戶輸入以下參數,這將是 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
編輯:正如@RobG 在評論中所說,我在示例中使用了這樣的日期格式:
2015-12-01
犯了一個錯誤。 並非所有瀏覽器都解釋使用“-”的日期格式。 最好使用“/”字符代替。
您需要使用setMonth
和getMonth
方法:
var start = new Date("2015/01/15");
var end = new Date("2016/12/15");
while (start <= end) {
console.log( new Date(start) );
start.setMonth( start.getMonth() + 1 );
}
更動態的解決方案:
function getDatesBtween(from, to, day){
var from = new Date(from),
to = new Date(to),
dates = [];
from.setDate(day);
to.setDate(day);
while(from <= to){
dates.push(new Date(from));
from.setMonth( from.getMonth() + 1 );
}
return dates;
}
var dates = getDatesBtween("2015/01/15", "2016/12/15", 15);
console.log(dates);
編輯 2正如@HBP 在評論中提到的,上述解決方案沒有考慮邊緣情況,並且不適用於一個月的最后幾天(一個月的第 29、30 和 31 天)。 例如,在某些情況下為
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){
var range1 = new Date(date1),
range2 = new Date(date2),
date1 = setDate(date1, day),
date2 = setDate(date2, day),
dates = [],
temp = null;
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;
};
}
var manager = new DateManager();
var 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
$("#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>
我認為 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>");
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.