![](/img/trans.png)
[英]How to count occurrences of dates in an array of date ranges: JavaScript
[英]How to get date ranges from an array of static dates in jquery or javascript
我想從數組的靜態日期創建日期范圍。
這是我想要的示例:
var collDates = [
"2017/01/01",
"2017/01/02",
"2017/01/03",
"2017/01/04",
"2017/01/08",
"2017/01/09"
];
這應該轉換為:
[
{ start_date: "2017/01/01", end_date: "2017/01/04" },
{ start_date: "2017/01/08", end_date: "2017/01/09" }
];
我已經解決了這個問題,這是示例代碼。
var staticDates = ["2017/01/01", "2017/01/02", "2017/01/03", "2017/01/04", "2017/01/08", "2017/01/09", "2017/01/10", "2017/01/11", "2017/01/12", "2017/01/13", "2017/01/14", "2017/01/15", "2017/01/16", "2017/01/17", "2017/01/18", "2017/01/19", "2017/01/20", "2017/01/21", "2017/01/22", "2017/01/23", "2017/01/24", "2017/01/25", "2017/01/26", "2017/01/27", "2017/01/28", "2017/01/29"];
var coll_dateIntervals = [];
var arr_temp = [];
var i = 1;
$.each(staticDates, function(index, moment_date){
//Day difference in # of days
var diff = Math.abs(moment(staticDates[index]).diff(staticDates[index + 1], "days"));
arr_temp.push(moment_date);
//Check the date difference in days.
if(diff <= 1 && diff !== undefined){
//If the difference is 1, than add the date to the temporary array
arr_temp.push(moment_date);
//If it's more than 1 day, or the last object
} else if (diff > 1 || diff === undefined){
//Store the interval in an object
console.log(arr_temp[arr_temp.length - 1]);
var obj_dateInterval = { start_date: moment(arr_temp[0], "YYYY/MM/DD").format('YYYY-MM-DD'), end_date: moment(arr_temp[arr_temp.length - 1], "YYYY/MM/DD").format('YYYY-MM-DD')};
coll_dateIntervals.push(obj_dateInterval);
//Empty the array to start the new loop
arr_temp = [];
}
});
console.log(coll_dateIntervals);
沒有庫,您只需要幾個簡單的函數即可進行解析和格式化。 對於無效的日期不做任何事情,但是如果需要的話,添加也不難。
逐步瀏覽日期以創建范圍。 從開始日期開始,將第二天計算為當前日期加一。 當得到的日期不是第二天時,請結束范圍並開始新的日期。 過期時也結束。
對於單個日期,創建一個單個日期范圍。
/* Parse date in format y/m/d ** @param {string} s - string to parse ** @returns {Date} */ function parseYMD(s) { var b = s.split(/\\D/); return new Date(b[0], b[1] - 1, b[2]) } /* Collate array of date strings in y/m/d format to ranges ** @param {Array} dateArray - array of dates to parse in y/m/d format ** @returns {Array} ranges in format {start_date: yyyy/mm/dd, end_date: yyyy/mm/dd} */ function datesToRanges(dateArray) { return dateArray.reduce(function(acc, currentDate, index) { var d = parseYMD(currentDate); // Start of a processing, initialise accumulator if (!acc) { acc = {range: {start_date: currentDate}, ranges: []}; // If not continuing range, end current range, store and start new } else if (acc.previousDate.setDate(acc.previousDate.getDate() + 1) != +d) { acc.range.end_date = acc.previousString; acc.ranges.push(acc.range); acc.range = {start_date: currentDate}; } // Keep current values for next iteration acc.previousDate = d; acc.previousString = currentDate; // If at end of dates, end current range if (index == dateArray.length - 1) { acc.range.end_date = currentDate; acc.ranges.push(acc.range); } return acc; }, null).ranges; } // Tests var collDates = [ "2017/01/01", // Start 4 day range "2017/01/02", "2017/01/03", "2017/01/04", // End 4 day range "2017/01/06", // Zero day range "2017/01/08", // Start one day range "2017/01/09", // End one day range "2017/01/15", // Zero day range ]; console.log(datesToRanges(collDates));
聚苯乙烯
無法看到這與jQuery有什么關系。 沒有moment.js標記,因此我沒有使用過。 ;-)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.